2658. Maximum Number of Fish in a Grid Medium

@problem@discussion
#Array#Depth-First Search#Breadth-First Search#Union Find#Matrix



1/**
2 * [2658] Maximum Number of Fish in a Grid
3 *
4 * You are given a 0-indexed 2D matrix grid of size m x n, where (r, c) represents:
5 * 
6 * 	A land cell if grid[r][c] = 0, or
7 * 	A water cell containing grid[r][c] fish, if grid[r][c] > 0.
8 * 
9 * A fisher can start at any water cell (r, c) and can do the following operations any number of times:
10 * 
11 * 	Catch all the fish at cell (r, c), or
12 * 	Move to any adjacent water cell.
13 * 
14 * Return the maximum number of fish the fisher can catch if he chooses his starting cell optimally, or 0 if no water cell exists.
15 * 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.
16 *  
17 * <strong class="example">Example 1:
18 * <img alt="" src="https://assets.leetcode.com/uploads/2023/03/29/example.png" style="width: 241px; height: 161px;" />
19 * Input: grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]
20 * Output: 7
21 * Explanation: The fisher can start at cell (1,3) and collect 3 fish, then move to cell (2,3) and collect 4 fish.
22 * 
23 * <strong class="example">Example 2:
24 * <img alt="" src="https://assets.leetcode.com/uploads/2023/03/29/example2.png" />
25 * Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]
26 * Output: 1
27 * Explanation: The fisher can start at cells (0,0) or (3,3) and collect a single fish. 
28 * 
29 *  
30 * Constraints:
31 * 
32 * 	m == grid.length
33 * 	n == grid[i].length
34 * 	1 <= m, n <= 10
35 * 	0 <= grid[i][j] <= 10
36 * 
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/maximum-number-of-fish-in-a-grid/
41// discuss: https://leetcode.com/problems/maximum-number-of-fish-in-a-grid/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46    pub fn find_max_fish(grid: Vec<Vec<i32>>) -> i32 {
47        0
48    }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_2658() {
59    }
60}
61


Back
© 2025 bowen.ge All Rights Reserved.