-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount Sub Islands
More file actions
28 lines (23 loc) · 798 Bytes
/
Count Sub Islands
File metadata and controls
28 lines (23 loc) · 798 Bytes
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
class Solution(object):
def countSubIslands(self, grid1, grid2):
rows=len(grid1)
cols=len(grid1[0])
def dfs(row,col):
if row<0 or row>=rows or col<0 or col>=cols or grid2[row][col]==0:
return
grid2[row][col]=0
dfs(row+1,col)
dfs(row-1,col)
dfs(row,col+1)
dfs(row,col-1)
for row in range(rows):
for col in range(cols):
if grid2[row][col]==1 and grid1[row][col]==0:
dfs(row,col)
count=0
for row in range(rows):
for col in range(cols):
if grid2[row][col]==1:
dfs(row,col)
count+=1
return count