Skip to content

Commit 8ffe35f

Browse files
committed
Sync LeetCode submission Runtime - 38 ms (64.63%), Memory - 18 MB (49.02%)
1 parent ac24c41 commit 8ffe35f

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>m x n</code>, where <code>(r, c)</code> represents:</p>
2+
3+
<ul>
4+
<li>A <strong>land</strong> cell if <code>grid[r][c] = 0</code>, or</li>
5+
<li>A <strong>water</strong> cell containing <code>grid[r][c]</code> fish, if <code>grid[r][c] &gt; 0</code>.</li>
6+
</ul>
7+
8+
<p>A fisher can start at any <strong>water</strong> cell <code>(r, c)</code> and can do the following operations any number of times:</p>
9+
10+
<ul>
11+
<li>Catch all the fish at cell <code>(r, c)</code>, or</li>
12+
<li>Move to any adjacent <strong>water</strong> cell.</li>
13+
</ul>
14+
15+
<p>Return <em>the <strong>maximum</strong> number of fish the fisher can catch if he chooses his starting cell optimally, or </em><code>0</code> if no water cell exists.</p>
16+
17+
<p>An <strong>adjacent</strong> cell of the cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> or <code>(r - 1, c)</code> if it exists.</p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
<img alt="" src="https://assets.leetcode.com/uploads/2023/03/29/example.png" style="width: 241px; height: 161px;" />
22+
<pre>
23+
<strong>Input:</strong> grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]
24+
<strong>Output:</strong> 7
25+
<strong>Explanation:</strong> The fisher can start at cell <code>(1,3)</code> and collect 3 fish, then move to cell <code>(2,3)</code>&nbsp;and collect 4 fish.
26+
</pre>
27+
28+
<p><strong class="example">Example 2:</strong></p>
29+
<img alt="" src="https://assets.leetcode.com/uploads/2023/03/29/example2.png" />
30+
<pre>
31+
<strong>Input:</strong> grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]
32+
<strong>Output:</strong> 1
33+
<strong>Explanation:</strong> The fisher can start at cells (0,0) or (3,3) and collect a single fish.
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>m == grid.length</code></li>
41+
<li><code>n == grid[i].length</code></li>
42+
<li><code>1 &lt;= m, n &lt;= 10</code></li>
43+
<li><code>0 &lt;= grid[i][j] &lt;= 10</code></li>
44+
</ul>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Approach 1: Depth-First Search
2+
3+
# Time: O(m * n)
4+
# Space: O(m * n)
5+
6+
class Solution:
7+
def calculate_fishes(self, grid, visited, row, col):
8+
if (row < 0
9+
or row >= len(grid)
10+
or col < 0
11+
or col >= len(grid[0])
12+
or grid[row][col] == 0
13+
or visited[row][col]
14+
):
15+
return 0
16+
17+
visited[row][col] = True
18+
19+
return (
20+
grid[row][col]
21+
+ self.calculate_fishes(grid, visited, row, col + 1)
22+
+ self.calculate_fishes(grid, visited, row, col - 1)
23+
+ self.calculate_fishes(grid, visited, row + 1, col)
24+
+ self.calculate_fishes(grid, visited, row - 1, col)
25+
)
26+
27+
def findMaxFish(self, grid: List[List[int]]) -> int:
28+
rows, cols = len(grid), len(grid[0])
29+
max_fish_count = 0
30+
31+
visited = [[False] * cols for _ in range(rows)]
32+
33+
for row in range(rows):
34+
for col in range(cols):
35+
if grid[row][col] > 0 and not visited[row][col]:
36+
max_fish_count = max(max_fish_count, self.calculate_fishes(grid, visited, row, col))
37+
38+
return max_fish_count
39+

0 commit comments

Comments
 (0)