Skip to content

Instantly share code, notes, and snippets.

@vrat28
Created June 2, 2021 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrat28/59c59369a3534b6e87cb56164794a9e1 to your computer and use it in GitHub Desktop.
Save vrat28/59c59369a3534b6e87cb56164794a9e1 to your computer and use it in GitHub Desktop.
Max Area (DFS,Recursive,Py)
class Solution(object):
def maxAreaOfIsland(self, grid):
seen = set()
def area(r, c):
if not (0 <= r < len(grid) and 0 <= c < len(grid[0])
and (r, c) not in seen and grid[r][c]):
return 0
seen.add((r, c))
return (1 + area(r+1, c) + area(r-1, c) +
area(r, c-1) + area(r, c+1))
return max(area(r, c)
for r in range(len(grid))
for c in range(len(grid[0])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment