Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 0-indexed 2D matrix grid of size m x n, where (r, c) represents:
grid[r][c] = 0, orgrid[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:
(r, c), orReturn 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:
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:
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.lengthn == grid[i].length1 <= m, n <= 100 <= grid[i][j] <= 10Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Union-Find
[[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]
[[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]
number-of-islands)max-area-of-island)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2658: Maximum Number of Fish in a Grid
class Solution {
private int[][] grid;
private int m;
private int n;
public int findMaxFish(int[][] grid) {
m = grid.length;
n = grid[0].length;
this.grid = grid;
int ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] > 0) {
ans = Math.max(ans, dfs(i, j));
}
}
}
return ans;
}
private int dfs(int i, int j) {
int cnt = grid[i][j];
grid[i][j] = 0;
int[] dirs = {-1, 0, 1, 0, -1};
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k], y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0) {
cnt += dfs(x, y);
}
}
return cnt;
}
}
// Accepted solution for LeetCode #2658: Maximum Number of Fish in a Grid
func findMaxFish(grid [][]int) (ans int) {
m, n := len(grid), len(grid[0])
dirs := [5]int{-1, 0, 1, 0, -1}
var dfs func(i, j int) int
dfs = func(i, j int) int {
cnt := grid[i][j]
grid[i][j] = 0
for k := 0; k < 4; k++ {
x, y := i+dirs[k], j+dirs[k+1]
if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0 {
cnt += dfs(x, y)
}
}
return cnt
}
for i := range grid {
for j := range grid[i] {
if grid[i][j] > 0 {
ans = max(ans, dfs(i, j))
}
}
}
return
}
# Accepted solution for LeetCode #2658: Maximum Number of Fish in a Grid
class Solution:
def findMaxFish(self, grid: List[List[int]]) -> int:
def dfs(i: int, j: int) -> int:
cnt = grid[i][j]
grid[i][j] = 0
for a, b in pairwise((-1, 0, 1, 0, -1)):
x, y = i + a, j + b
if 0 <= x < m and 0 <= y < n and grid[x][y]:
cnt += dfs(x, y)
return cnt
m, n = len(grid), len(grid[0])
ans = 0
for i in range(m):
for j in range(n):
if grid[i][j]:
ans = max(ans, dfs(i, j))
return ans
// Accepted solution for LeetCode #2658: Maximum Number of Fish in a Grid
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2658: Maximum Number of Fish in a Grid
// class Solution {
// private int[][] grid;
// private int m;
// private int n;
//
// public int findMaxFish(int[][] grid) {
// m = grid.length;
// n = grid[0].length;
// this.grid = grid;
// int ans = 0;
// for (int i = 0; i < m; ++i) {
// for (int j = 0; j < n; ++j) {
// if (grid[i][j] > 0) {
// ans = Math.max(ans, dfs(i, j));
// }
// }
// }
// return ans;
// }
//
// private int dfs(int i, int j) {
// int cnt = grid[i][j];
// grid[i][j] = 0;
// int[] dirs = {-1, 0, 1, 0, -1};
// for (int k = 0; k < 4; ++k) {
// int x = i + dirs[k], y = j + dirs[k + 1];
// if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0) {
// cnt += dfs(x, y);
// }
// }
// return cnt;
// }
// }
// Accepted solution for LeetCode #2658: Maximum Number of Fish in a Grid
function findMaxFish(grid: number[][]): number {
const m = grid.length;
const n = grid[0].length;
const dirs = [-1, 0, 1, 0, -1];
const dfs = (i: number, j: number): number => {
let cnt = grid[i][j];
grid[i][j] = 0;
for (let k = 0; k < 4; ++k) {
const x = i + dirs[k];
const y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0) {
cnt += dfs(x, y);
}
}
return cnt;
};
let ans = 0;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (grid[i][j] > 0) {
ans = Math.max(ans, dfs(i, j));
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Track components with a list or adjacency matrix. Each union operation may need to update all n elements’ component labels, giving O(n) per union. For n union operations total: O(n²). Find is O(1) with direct lookup, but union dominates.
With path compression and union by rank, each find/union operation takes O(α(n)) amortized time, where α is the inverse Ackermann function — effectively constant. Space is O(n) for the parent and rank arrays. For m operations on n elements: O(m × α(n)) total.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.