Skip to content

Latest commit

 

History

History
51 lines (38 loc) · 1.63 KB

_2658. Maximum Number of Fish in a Grid.md

File metadata and controls

51 lines (38 loc) · 1.63 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : January 28, 2025

Last updated : January 28, 2025


Related Topics : Array, Depth-First Search, Breadth-First Search, Union Find, Matrix

Acceptance Rate : 70.74 %


Solutions

Python

class Solution:
    def traverse_waterpocket(self, r: int, c: int, grid: List[List[int]]) -> int :
        output, grid[r][c] = grid[r][c], 0
        if r - 1 >= 0 and grid[r - 1][c] :
            output += self.traverse_waterpocket(r - 1, c, grid)
        if c - 1 >= 0 and grid[r][c - 1] :
            output += self.traverse_waterpocket(r, c - 1, grid)
        if r + 1 < len(grid) and grid[r + 1][c] :
            output += self.traverse_waterpocket(r + 1, c, grid)
        if c + 1 < len(grid[0]) and grid[r][c + 1] :
            output += self.traverse_waterpocket(r, c + 1, grid)
        return output

    def findMaxFish(self, grid: List[List[int]]) -> int:
        maxx = 0

        for r in range(len(grid)) :
            for c in range(len(grid[0])) :
                if not grid[r][c] :
                    continue
                maxx = max(maxx, self.traverse_waterpocket(r, c, grid))

        return maxx