#2658
Maximum Number of Fish in a Grid
pupil · 560 · lc medium +28 · verified · 70.5% accepted · 951 likes · top 79%
Description
You are given a 0-indexed m x n matrix grid where grid[r][c] = 0 is land and grid[r][c] > 0 is a water cell holding that many fish. A fisher starts at any water cell, catches all fish there, and can move to adjacent (up/down/left/right) water cells. Return the maximum fish catchable with an optimal start, or 0 if no water cells exist.
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.
Code
1
2
3