[LeetCode] 2658. Maximum Number of Fish in a Grid

You are given a 0-indexed 2D matrix grid of size m x n, where (r, c) represents:
A land cell if grid[r][c] = 0, or
A water cell containing grid[r][c] fish, if grid[r][c] > 0.
A fisher can start at any water cell (r, c) and can do the following operations any number of times:

Catch all the fish at cell (r, c), or
Move to any adjacent water cell.
Return the maximum number of fish the fisher can catch if he chooses his starting cell optimally, or 0 if no water cell exists.

An adjacent cell of the cell (r, c), is one of the cells (r, c + 1), (r, c - 1), (r + 1, c) or (r - 1, c) if it exists.

Example 1:
Example 1
Input: grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]
Output: 7
Explanation: The fisher can start at cell (1,3) and collect 3 fish, then move to cell (2,3) and collect 4 fish.

Example 2:
Example 2
Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]
Output: 1
Explanation: The fisher can start at cells (0,0) or (3,3) and collect a single fish.

Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 10
0 <= grid[i][j] <= 10

网格图中鱼的最大数目。

给你一个下标从 0 开始大小为 m x n 的二维整数数组 grid ,其中下标在 (r, c) 处的整数表示:

如果 grid[r][c] = 0 ,那么它是一块 陆地 。
如果 grid[r][c] > 0 ,那么它是一块 水域 ,且包含 grid[r][c] 条鱼。
一位渔夫可以从任意 水域 格子 (r, c) 出发,然后执行以下操作任意次:

捕捞格子 (r, c) 处所有的鱼,或者
移动到相邻的 水域 格子。
请你返回渔夫最优策略下, 最多 可以捕捞多少条鱼。如果没有水域格子,请你返回 0 。

格子 (r, c) 相邻 的格子为 (r, c + 1) ,(r, c - 1) ,(r + 1, c) 和 (r - 1, c) ,前提是相邻格子在网格图内。

思路

这道题是 flood fill 类型的题目。这道题是让我们在矩阵内找一个面积最大的岛屿。
695题几乎是一模一样的。

复杂度

时间O(mn)
空间O(n)

代码

BFS实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
int m;
int n;
int[][] dirs = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };

public int findMaxFish(int[][] grid) {
m = grid.length;
n = grid[0].length;
int res = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] > 0) {
res = Math.max(res, bfs(grid, i, j));
}
}
}
return res;
}

private int bfs(int[][] grid, int i, int j) {
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[] { i, j });
int count = grid[i][j];
grid[i][j] = 0;
while (!queue.isEmpty()) {
int[] cur = queue.poll();
for (int[] dir : dirs) {
int x = cur[0] + dir[0];
int y = cur[1] + dir[1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0) {
queue.offer(new int[] { x, y });
count += grid[x][y];
grid[x][y] = 0;
}
}
}
return count;
}
}

DFS实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
int m;
int n;

public int findMaxFish(int[][] grid) {
m = grid.length;
n = grid[0].length;
int res = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] > 0) {
res = Math.max(res, dfs(grid, i, j));
}
}
}
return res;
}

private int dfs(int[][] grid, int i, int j) {
if (i < 0 || j < 0 || i >= m || j >= n || grid[i][j] == 0) {
return 0;
}
int count = grid[i][j];
grid[i][j] = 0;
count += dfs(grid, i - 1, j);
count += dfs(grid, i + 1, j);
count += dfs(grid, i, j - 1);
count += dfs(grid, i, j + 1);
return count;
}
}

相关题目

1
2
695. Max Area of Island
2658. Maximum Number of Fish in a Grid

[LeetCode] 2658. Maximum Number of Fish in a Grid
https://shurui91.github.io/posts/2867469525.html
Author
Aaron Liu
Posted on
February 16, 2025
Licensed under