-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax-area-of-island.py
33 lines (30 loc) · 1000 Bytes
/
max-area-of-island.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
R = len(grid)
C = len(grid[0])
LAND = 1
WATER = 0
self.max_area = 0
visited = [[False for _ in range(C)] for _ in range(R)]
def inbound(row,col):
return 0<=row<R and 0<=col<C
max_area = 0
def dfs(row,col):
nonlocal max_area
if inbound(row,col) and not visited[row][col]:
visited[row][col] = True
if grid[row][col] == 1:
max_area += 1
dfs(row-1,col)
dfs(row,col-1)
dfs(row,col+1)
dfs(row+1,col)
return max_area
answer = 0
for i in range(R):
for j in range(C):
if not visited[i][j]:
max_area = 0
dfs(i,j)
answer = max(answer,max_area)
return answer