Maximum Fish in a Grid
Updated
Problem statement
A rectangular integer matrix grid describes land and water: 0 is land, and each positive value is the fish count in a water cell.
Choose any water cell as a starting point. You may collect all fish in a visited cell and move repeatedly between water cells sharing an edge. A cell’s fish can be collected only once.
Return the largest total obtainable from one starting point, or 0 if every cell is land. Diagonal movement and movement through land are forbidden.
Function
findMaxFish(grid: int[][]) → intExamples
Example 1
grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]return = 7The component with 3 and 4 fish totals 7, more than each other component.
Example 2
grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]return = 1The two positive cells are disconnected; either start collects 1.
Example 3
grid = [[2,0,3],[4,5,0]]return = 11The connected cells 2,4 and 5 total 11; the 3-fish cell is isolated.
Constraints
1 <= grid.length <= 10.1 <= grid[r].length <= 10; all rows have equal length.0 <= grid[r][c] <= 10.
More NVIDIA Onsite Interview questions
- Check Endianness Of A Byte ArrayONSITE INTERVIEW · Seen Sep 2026
- Implement MallocONSITE INTERVIEW · Seen Sep 2026
- Implement PrintfONSITE INTERVIEW · Seen Sep 2026
- Merge Two Arrays Into Sorted OrderONSITE INTERVIEW · Seen Sep 2026
- Middle Node Of A Linked ListONSITE INTERVIEW · Seen Sep 2026
- Ping Reply TrackingONSITE INTERVIEW · Seen Sep 2026
- Reverse A StringONSITE INTERVIEW · Seen Sep 2026
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- First Register Overflow TickONSITE INTERVIEW · Seen May 2024
- Two-Egg DroppingONSITE INTERVIEW · Seen May 2024
Similar questions at other companies
- Number of IslandsFleetx · ONSITE INTERVIEW
- Number of IslandsIntuit · ONSITE INTERVIEWIntuit interview questions
- Number of IslandsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Shortest Bridge Between Two IslandsCompass International Holdings · ONSITE INTERVIEWCompass International Holdings interview questions
- Number of Islands with Diagonal ConnectivityGoogle · ONSITE INTERVIEWGoogle interview questions
- Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions
- Number of Closed IslandsSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Foreground Region SizesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- The MazeGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Mouse and Cheese ReachabilityMeta · ONSITE INTERVIEWMeta interview questions