Skip to content

Commit 4a8c954

Browse files
committed
Jan 28
1 parent a357eec commit 4a8c954

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from itertools import product
2+
from typing import List
3+
4+
5+
class Solution:
6+
DIRS = ((0, 1), (1, 0), (0, -1), (-1, 0))
7+
8+
def findMaxFish(self, grid: List[List[int]]) -> int:
9+
m, n = len(grid), len(grid[0])
10+
11+
def dfs(x: int, y: int) -> int:
12+
if x < 0 or x >= m or y < 0 or y >= n or grid[x][y] == 0:
13+
return 0
14+
fish = grid[x][y]
15+
grid[x][y] = 0
16+
for dx, dy in self.DIRS:
17+
fish += dfs(x + dx, y + dy)
18+
return fish
19+
20+
max_fish = 0
21+
for i, j in product(range(m), range(n)):
22+
if grid[i][j] != 0:
23+
max_fish = max(max_fish, dfs(i, j))
24+
return max_fish
25+
26+
27+
def main():
28+
grid = [[0, 2, 1, 0],
29+
[4, 0, 0, 3],
30+
[1, 0, 0, 4],
31+
[0, 3, 2, 0]]
32+
assert Solution().findMaxFish(grid) == 7
33+
34+
grid = [[1, 0, 0, 0],
35+
[0, 0, 0, 0],
36+
[0, 0, 0, 0],
37+
[0, 0, 0, 1]]
38+
assert Solution().findMaxFish(grid) == 1
39+
40+
41+
if __name__ == '__main__':
42+
main()

2025-01-January-LeetCoding-Challenge/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
| January 25 | [2948. Make Lexicographically Smallest Array by Swapping Elements](https://leetcode.com/problems/make-lexicographically-smallest-array-by-swapping-elements/) | Medium | Solved |
3131
| January 26 | [2127. Maximum Employees to Be Invited to a Meeting](https://leetcode.com/problems/maximum-employees-to-be-invited-to-a-meeting/) | Hard | Unsolved |
3232
| January 27 | [1462. Course Schedule IV](https://leetcode.com/problems/course-schedule-iv/) | Medium | Solved |
33-
| January 28 | []() | | |
33+
| January 28 | [2658. Maximum Number of Fish in a Grid](https://leetcode.com/problems/maximum-number-of-fish-in-a-grid/) | Medium | Solved |
3434
| January 29 | []() | | |
3535
| January 30 | []() | | |
3636
| January 31 | []() | | |
@@ -40,5 +40,5 @@
4040
| Level | Problems | Solved | Unsolved |
4141
| --- | --- | --- | --- |
4242
| Easy | 4 | 4 | 0 |
43-
| Medium | 19 | 16 | 3 |
43+
| Medium | 20 | 17 | 3 |
4444
| Hard | 3 | 0 | 3 |

0 commit comments

Comments
 (0)