Created
June 2, 2021 03:58
-
-
Save vrat28/59c59369a3534b6e87cb56164794a9e1 to your computer and use it in GitHub Desktop.
Max Area (DFS,Recursive,Py)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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