2026-06-01 18:12:40 +08:00
|
|
|
#+ANKI_DECK: study_deck_02
|
2026-06-01 17:12:10 +08:00
|
|
|
* TODO 2658. Maximum Number of Fish in a Grid :medium:
|
2026-06-01 02:33:30 +08:00
|
|
|
:PROPERTIES:
|
2026-06-01 17:22:07 +08:00
|
|
|
:NEETCODE: [[file:../../roadmap.org::*2658. Maximum Number of Fish in a Grid][2658. Maximum Number of Fish in a Grid]]
|
2026-06-01 02:33:30 +08:00
|
|
|
:END:
|
|
|
|
|
|
2026-06-01 17:22:07 +08:00
|
|
|
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:*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#+begin_src
|
|
|
|
|
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.
|
|
|
|
|
#+end_src
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*Example 2:*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#+begin_src
|
|
|
|
|
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.
|
|
|
|
|
#+end_src
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*Constraints:*
|
|
|
|
|
|
|
|
|
|
- ~m == grid.length~
|
|
|
|
|
|
|
|
|
|
- ~n == grid[i].length~
|
|
|
|
|
|
|
|
|
|
- ~1 <= m, n <= 10~
|
|
|
|
|
|
|
|
|
|
- ~0 <= grid[i][j] <= 10~
|
|
|
|
|
|
2026-06-01 02:39:53 +08:00
|
|
|
** TODO Approach
|
|
|
|
|
Write your approach here.
|
|
|
|
|
|
|
|
|
|
** TODO Python
|
2026-06-05 22:32:49 +08:00
|
|
|
#+begin_src python :lc-problem 2658 :lc-lang python3
|
2026-06-01 17:22:07 +08:00
|
|
|
class Solution:
|
|
|
|
|
def findMaxFish(self, grid: List[List[int]]) -> int:
|
2026-06-01 02:39:53 +08:00
|
|
|
#+end_src
|
|
|
|
|
|
|
|
|
|
** TODO C++
|
2026-06-05 22:32:49 +08:00
|
|
|
#+begin_src cpp :lc-problem 2658
|
2026-06-01 17:22:07 +08:00
|
|
|
class Solution {
|
|
|
|
|
public:
|
|
|
|
|
int findMaxFish(vector<vector<int>>& grid) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
};
|
2026-06-01 02:33:30 +08:00
|
|
|
#+end_src
|