Problem
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, ifgrid[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.
Examples
Example 1:
$$ \begin{array}{|c|c|c|c|} \hline 0 & 2 & 1 & 0 \\ \hline 4 & 0 & 0 & 3 \\ \hline 1 & 0 & 0 & 4 \\ \hline 0 & 3 & 2 & 0 \\ \hline \end{array} $$
|
|
Example 2:
$$ \begin{array}{|c|c|c|c|} \hline 1 & 0 & 0 & 0 \\ \hline 0 & 0 & 0 & 0 \\ \hline 0 & 0 & 0 & 0 \\ \hline 0 & 0 & 0 & 1 \\ \hline \end{array} $$
|
|
Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 10
0 <= grid[i][j] <= 10
Solution
Method 1 - DFS
- To solve the problem of finding the maximum number of fish a fisher can catch by starting from any water cell and moving to adjacent water cells, we can approach it as a graph traversal problem.
- We will use Depth-First Search (DFS) to explore all possible water cells from a given starting point and calculate the total fish that can be caught.]
Approach
- Graph Representation and Traversal:
- Each cell in the grid is a node.
- There are edges between nodes if they are adjacent and both contain water.
- DFS Implementation:
- Perform DFS starting from each water cell, marking visited cells to avoid recounting.
- For each DFS traversal, track the number of fish caught.
- Update the global maximum whenever a higher count is found.
- Edge Cases:
- If there are no water cells at all, return 0.
Video explanation
Here is the video explaining this method in detail. Please check it out:
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(m * n)
wherem
is the number of rows andn
is the number of columns. Each cell is visited once. - 🧺 Space complexity:
O(m * n)
for the recursion stack and the visited matrix.