Simple python solution using DFS


  • 0
    class Solution:
        def maxAreaOfIsland(self, grid):
            return max([self.dfs(r, c, grid) for r in range(len(grid)) for c in range(len(grid[0]))])
    
        def dfs(self, r, c, grid):
            if not (0 <= r < len(grid) and (0 <= c < len(grid[0])) and grid[r][c]):
                return 0
            grid[r][c] = 0
            return 1 + sum([self.dfs(r + x[0], c + x[1], grid) for x in [(0, 1), (0, -1), (1, 0), (-1, 0)]])
    

  • 0
    O

    so clean and elegant...


  • 0
    R

    genius solution


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.