-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17.py
30 lines (26 loc) · 791 Bytes
/
day17.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
#number of islands. A GRAPH QUESTION(GOOGLE)
def numIslands(grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
count = 0
if not grid:
return count
y_max = len(grid)
x_max = len(grid[0])
def helper(x,y):
if 0<=x<x_max and 0<=y<y_max and grid[y][x]=="1":
grid[y][x]="0"
helper(x+1,y)
helper(x-1,y)
helper(x,y+1)
helper(x,y-1)
for y_idx in range(y_max):
for x_idx in range(x_max):
if grid[y_idx][x_idx]=="1":
count+=1
helper(x_idx,y_idx)
return count
print(numIslands([["1","0","1","1","0","1","1"]]))
print(numIslands([["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]))