Skip to content

Commit 816fa2e

Browse files
committed
200, 1444
1 parent 1785c2e commit 816fa2e

5 files changed

Lines changed: 260 additions & 2 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
| 121 | Best Time to Buy and Sell Stock | [Ruby](./algorithms/ruby/0121-best-time-to-buy-and-sell-stock.rb) | Easy |
4646
| 129 | Sum Root to Leaf Numbers | [Ruby](./algorithms/ruby/0129-sum-root-to-leaf-numbers.rb) | Medium |
4747
| 142 | Linked List Cycle II | [Ruby](./algorithms/ruby/0142-linked-list-cycle-ii.rb) | Medium |
48+
| 200 | Number of Islands | [Ruby](./algorithms/ruby/0200-number-of-islands.rb) | Medium |
4849
| 205 | Isomorphic Strings | [Ruby](./algorithms/ruby/0205-isomorphic-strings.rb) | Easy |
4950
| 206 | Reverse Linked List | [Ruby](./algorithms/ruby/0206-reverse-linked-list.rb) | Easy |
5051
| 208 | Implement Trie (Prefix Tree) | [Ruby](./algorithms/ruby/0208-implement-trie-prefix-tree.rb) | Medium |
@@ -90,6 +91,7 @@
9091
| 1319 | Number of Operations to Make Network Connected | [Ruby](./algorithms/ruby/1319-number-of-operations-to-make-network-connected.rb) | Medium |
9192
| 1345 | Jump Game IV | [Ruby](./algorithms/ruby/1345-jump-game-iv.rb) | Hard |
9293
| 1402 | Reducing Dishes | [Ruby](./algorithms/ruby/1402-reducing-dishes.rb) | Hard |
94+
| 1444 | Number of Ways of Cutting a Pizza | [Ruby](./algorithms/ruby/1444-number-of-ways-of-cutting-a-pizza.rb) [Python3](./algorithms/python3/1444-number-of-ways-of-cutting-a-pizza.py) | Hard |
9395
| 1466 | Reorder Routes to Make All Paths Lead to the City Zero | [Ruby](./algorithms/ruby/1466-eorder-routes-to-make-all-paths-lead-to-the-city-zero.rb) | Medium |
9496
| 1470 | Shuffle the Array | [Ruby](./algorithms/ruby/1470-shuffle-the-array.rb) | Easy |
9597
| 1472 | Design Browser History | [Ruby](./algorithms/ruby/1472-design-browser-history.rb) | Medium |
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# 1444. Number of Ways of Cutting a Pizza
2+
# https://leetcode.com/problems/number-of-ways-of-cutting-a-pizza
3+
4+
from functools import lru_cache
5+
6+
class Solution:
7+
def ways(self, pizza: list[str], K: int) -> int:
8+
m, n, MOD = len(pizza), len(pizza[0]), 10 ** 9 + 7
9+
preSum = [[0] * (n + 1) for _ in range(m + 1)]
10+
11+
for r in range(m - 1, -1, -1):
12+
for c in range(n - 1, -1, -1):
13+
preSum[r][c] = preSum[r][c + 1] + preSum[r + 1][c] - preSum[r + 1][c + 1] + (pizza[r][c] == 'A')
14+
15+
@lru_cache(None)
16+
def dp(k, r, c):
17+
if preSum[r][c] == 0: return 0
18+
if k == 0: return 1
19+
ans = 0
20+
21+
for nr in range(r + 1, m):
22+
if preSum[r][c] - preSum[nr][c] > 0:
23+
ans = (ans + dp(k - 1, nr, c)) % MOD
24+
for nc in range(c + 1, n):
25+
if preSum[r][c] - preSum[r][nc] > 0:
26+
ans = (ans + dp(k - 1, r, nc)) % MOD
27+
28+
return ans
29+
30+
return dp(K - 1, 0, 0)
31+
32+
# ********************#
33+
# TEST #
34+
# ********************#
35+
36+
import unittest
37+
38+
class TestStringMethods(unittest.TestCase):
39+
def test_ways(self):
40+
self.assertEqual(Solution.ways(self, ["A..", "AAA", "..."], 3), 3)
41+
self.assertEqual(Solution.ways(self, ["A..", "AA.", "..."], 3), 1)
42+
self.assertEqual(Solution.ways(self, ["A..", "A..", "..."], 1), 1)
43+
44+
45+
if __name__ == '__main__':
46+
unittest.main()
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# frozen_string_literal: true
2+
3+
# 200. Number of Islands
4+
# https://leetcode.com/problems/number-of-islands
5+
6+
=begin
7+
8+
Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.
9+
10+
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
11+
12+
### Example 1:
13+
Input: grid = [
14+
["1","1","1","1","0"],
15+
["1","1","0","1","0"],
16+
["1","1","0","0","0"],
17+
["0","0","0","0","0"]
18+
]
19+
Output: 1
20+
21+
### Example 2:
22+
Input: grid = [
23+
["1","1","0","0","0"],
24+
["1","1","0","0","0"],
25+
["0","0","1","0","0"],
26+
["0","0","0","1","1"]
27+
]
28+
Output: 3
29+
30+
### Constraints:
31+
* m == grid.length
32+
* n == grid[i].length
33+
* 1 <= m, n <= 300
34+
* grid[i][j] is '0' or '1'.
35+
36+
=end
37+
38+
# Runtime: 180 ms
39+
# Memory: 219.5 MB
40+
# @param {Character[][]} grid
41+
# @return {Integer}
42+
def num_islands(grid)
43+
count = 0
44+
grid.size.times do |i|
45+
grid.first.size.times do |j|
46+
count += dfs(grid, i, j) if grid[i][j] == "1"
47+
end
48+
end
49+
count
50+
end
51+
52+
def dfs(grid, i, j)
53+
return 0 if i < 0 || j < 0 || i >= grid.size || j >= grid.first.size || grid[i][j] == "0"
54+
grid[i][j] = "0"
55+
dfs(grid, i - 1, j)
56+
dfs(grid, i + 1, j)
57+
dfs(grid, i, j - 1)
58+
dfs(grid, i, j + 1)
59+
1
60+
end
61+
62+
# **************** #
63+
# TEST #
64+
# **************** #
65+
66+
require "test/unit"
67+
class Test_num_islands < Test::Unit::TestCase
68+
def test_
69+
assert_equal 1, num_islands([["1", "1", "1", "1", "0"], ["1", "1", "0", "1", "0"], ["1", "1", "0", "0", "0"], ["0", "0", "0", "0", "0"]])
70+
assert_equal 3, num_islands([["1", "1", "0", "0", "0"], ["1", "1", "0", "0", "0"], ["0", "0", "1", "0", "0"], ["0", "0", "0", "1", "1"] ])
71+
end
72+
end

algorithms/ruby/0733-flood-fill.rb

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
11
# frozen_string_literal: true
22

3-
# Problem: 733. Flood Fill
4-
# URL: https://leetcode.com/problems/flood-fill
3+
# 733. Flood Fill
4+
# https://leetcode.com/problems/flood-fill
5+
6+
=begin
7+
8+
An image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image.
9+
10+
You are also given three integers sr, sc, and color. You should perform a flood fill on the image starting from the pixel image[sr][sc].
11+
12+
To perform a flood fill, consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color), and so on. Replace the color of all of the aforementioned pixels with color.
13+
14+
Return the modified image after performing the flood fill.
15+
16+
### Example 1:
17+
Input: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2
18+
Output: [[2,2,2],[2,2,0],[2,0,1]]
19+
Explanation: From the center of the image with position (sr, sc) = (1, 1) (i.e., the red pixel), all pixels connected by a path of the same color as the starting pixel (i.e., the blue pixels) are colored with the new color.
20+
Note the bottom corner is not colored 2, because it is not 4-directionally connected to the starting pixel.
21+
22+
### Example 2:
23+
Input: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0
24+
Output: [[0,0,0],[0,0,0]]
25+
Explanation: The starting pixel is already colored 0, so no changes are made to the image.
26+
27+
### Constraints:
28+
* m == image.length
29+
* n == image[i].length
30+
* 1 <= m, n <= 50
31+
* 0 <= image[i][j], color < 216
32+
* 0 <= sr < m
33+
* 0 <= sc < n
34+
35+
=end
536

637
# @param {Integer[][]} image
738
# @param {Integer} sr
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# frozen_string_literal: true
2+
3+
# 1444. Number of Ways of Cutting a Pizza
4+
# https://leetcode.com/problems/number-of-ways-of-cutting-a-pizza
5+
6+
=begin
7+
8+
Given a rectangular pizza represented as a rows x cols matrix containing the following characters: 'A' (an apple) and '.' (empty cell) and given the integer k. You have to cut the pizza into k pieces using k-1 cuts.
9+
10+
For each cut you choose the direction: vertical or horizontal, then you choose a cut position at the cell boundary and cut the pizza into two pieces. If you cut the pizza vertically, give the left part of the pizza to a person. If you cut the pizza horizontally, give the upper part of the pizza to a person. Give the last piece of pizza to the last person.
11+
12+
Return the number of ways of cutting the pizza such that each piece contains at least one apple. Since the answer can be a huge number, return this modulo 10^9 + 7.
13+
14+
### Example 1:
15+
Input: pizza = ["A..","AAA","..."], k = 3
16+
Output: 3
17+
Explanation: The figure above shows the three ways to cut the pizza. Note that pieces must contain at least one apple.
18+
19+
### Example 2:
20+
Input: pizza = ["A..","AA.","..."], k = 3
21+
Output: 1
22+
23+
### Example 3:
24+
Input: pizza = ["A..","A..","..."], k = 1
25+
Output: 1
26+
27+
### Constraints:
28+
29+
* 1 <= rows, cols <= 50
30+
* rows == pizza.length
31+
* cols == pizza[i].length
32+
* 1 <= k <= 10
33+
* pizza consists of characters 'A' and '.' only.
34+
35+
=end
36+
37+
# @param {String[]} pizza
38+
# @param {Integer} k
39+
# @return {Integer}
40+
def ways(pizza, k)
41+
rows = pizza.size
42+
cols = pizza[0].size
43+
apples = Array.new(rows + 1) { Array.new(cols + 1, 0) }
44+
f = Array.new(rows) { Array.new(cols, 0) }
45+
46+
row = rows - 1
47+
while row >= 0
48+
col = cols - 1
49+
while col >= 0
50+
apples[row][col] = ((pizza[row][col] == "A") && 1 || 0) + apples[row + 1][col] + apples[row][col + 1] - apples[row + 1][col + 1]
51+
f[row][col] = (apples[row][col] > 0) && 1 || 0
52+
col -= 1
53+
end
54+
row -= 1
55+
end
56+
57+
mod = 1000000007
58+
remain = 1
59+
while remain < k
60+
g = Array.new(rows) { Array.new(cols, 0) }
61+
row = 0
62+
while row < rows
63+
col = 0
64+
while col < cols
65+
next_row = row + 1
66+
while next_row < rows
67+
if apples[row][col] - apples[next_row][col] > 0
68+
g[row][col] += f[next_row][col] %= mod
69+
end
70+
next_row += 1
71+
end
72+
73+
next_col = col + 1
74+
while next_col < cols
75+
if apples[row][col] - apples[row][next_col] > 0
76+
g[row][col] += f[row][next_col] %= mod
77+
end
78+
next_col += 1
79+
end
80+
col += 1
81+
end
82+
row += 1
83+
end
84+
f = g
85+
remain += 1
86+
end
87+
88+
f[0][0]
89+
end
90+
91+
# **************** #
92+
# TEST #
93+
# **************** #
94+
95+
# require "test/unit"
96+
# class Test_ways < Test::Unit::TestCase
97+
# def test_
98+
# assert_equal 3, ways(["A..", "AAA", "..."], 3)
99+
# assert_equal 1, ways(["A..", "AA.", "..."], 3)
100+
# assert_equal 1, ways(["A..", "A..", "..."], 1)
101+
# end
102+
# end
103+
104+
105+
pizza = ["..A.A.AAA...AAAAAA.AA..A..A.A......A.AAA.AAAAAA.AA", "A.AA.A.....AA..AA.AA.A....AAA.A........AAAAA.A.AA.", "A..AA.AAA..AAAAAAAA..AA...A..A...A..AAA...AAAA..AA", "....A.A.AA.AA.AA...A.AA.AAA...A....AA.......A..AA.", "AAA....AA.A.A.AAA...A..A....A..AAAA...A.A.A.AAAA..", "....AA..A.AA..A.A...A.A..AAAA..AAAA.A.AA..AAA...AA", "A..A.AA.AA.A.A.AA..A.A..A.A.AAA....AAAAA.A.AA..A.A", ".AA.A...AAAAA.A..A....A...A.AAAA.AA..A.AA.AAAA.AA.", "A.AA.AAAA.....AA..AAA..AAAAAAA...AA.A..A.AAAAA.A..", "A.A...A.A...A..A...A.AAAA.A..A....A..AA.AAA.AA.AA.", ".A.A.A....AAA..AAA...A.AA..AAAAAAA.....AA....A....", "..AAAAAA..A..A...AA.A..A.AA......A.AA....A.A.AAAA.", "...A.AA.AAA.AA....A..AAAA...A..AAA.AAAA.A.....AA.A", "A.AAAAA..A...AAAAAAAA.AAA.....A.AAA.AA.A..A.A.A...", "A.A.AA...A.A.AA...A.AA.AA....AA...AA.A..A.AA....AA", "AA.A..A.AA..AAAAA...A..AAAAA.AA..AA.AA.A..AAAAA..A", "...AA....AAAA.A...AA....AAAAA.A.AAAA.A.AA..AA..AAA", "..AAAA..AA..A.AA.A.A.AA...A...AAAAAAA..A.AAA..AA.A", "AA....AA....AA.A......AAA...A...A.AA.A.AA.A.A.AA.A", "A.AAAA..AA..A..AAA.AAA.A....AAA.....A..A.AA.A.A...", "..AA...AAAAA.A.A......AA...A..AAA.AA..A.A.A.AA..A.", ".......AA..AA.AAA.A....A...A.AA..A.A..AAAAAAA.AA.A", ".A.AAA.AA..A.A.A.A.A.AA...AAAA.A.A.AA..A...A.AAA..", "A..AAAAA.A..A..A.A..AA..A...AAA.AA.A.A.AAA..A.AA..", "A.AAA.A.AAAAA....AA..A.AAA.A..AA...AA..A.A.A.AA.AA", ".A..AAAA.A.A.A.A.......AAAA.AA...AA..AAA..A...A.AA", "A.A.A.A..A...AA..A.AAA..AAAAA.AA.A.A.A..AA.A.A....", "A..A..A.A.AA.A....A...A......A.AA.AAA..A.AA...AA..", ".....A..A...A.A...A..A.AA.A...AA..AAA...AA..A.AAA.", "A...AA..A..AA.A.A.AAA..AA..AAA...AAA..AAA.AAAAA...", "AA...AAA.AAA...AAAA..A...A..A...AA...A..AA.A...A..", "A.AA..AAAA.AA.AAA.A.AA.A..AAAAA.A...A.A...A.AA....", "A.......AA....AA..AAA.AAAAAAA.A.AA..A.A.AA....AA..", ".A.A...AA..AA...AA.AAAA.....A..A..A.AA.A.AA...A.AA", "..AA.AA.AA..A...AA.AA.AAAAAA.....A.AA..AA......A..", "AAA..AA...A....A....AA.AA.AA.A.A.A..AA.AA..AAA.AAA", "..AAA.AAA.A.AA.....AAA.A.AA.AAAAA..AA..AA.........", ".AA..A......A.A.AAA.AAAA...A.AAAA...AAA.AAAA.....A", "AAAAAAA.AA..A....AAAA.A..AA.A....AA.A...A.A....A..", ".A.A.AA..A.AA.....A.A...A.A..A...AAA..A..AA..A.AAA", "AAAA....A...A.AA..AAA..A.AAA..AA.........AA.AAA.A.", "......AAAA..A.AAA.A..AAA...AAAAA...A.AA..A.A.AA.A.", "AA......A.AAAAAAAA..A.AAA...A.A....A.AAA.AA.A.AAA.", ".A.A....A.AAA..A..AA........A.AAAA.AAA.AA....A..AA", ".AA.A...AA.AAA.A....A.A...A........A.AAA......A...", "..AAA....A.A...A.AA..AAA.AAAAA....AAAAA..AA.AAAA..", "..A.AAA.AA..A.AA.A...A.AA....AAA.A.....AAA...A...A", ".AA.AA...A....A.AA.A..A..AAA.A.A.AA.......A.A...A.", "...A...A.AA.A..AAAAA...AA..A.A..AAA.AA...AA...A.A.", "..AAA..A.A..A..A..AA..AA...A..AA.AAAAA.A....A..A.A"]
106+
k = 8
107+
puts ways(pizza, k) # 641829390

0 commit comments

Comments
 (0)