-
Notifications
You must be signed in to change notification settings - Fork 0
/
islands.py
53 lines (36 loc) · 944 Bytes
/
islands.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# input is a 2d grip map of 1's and 0's and count the number of islands
# connected by 1's connect horizontally or vertically
# examples
# 1
# input
# 11110
# 11010
# 11000
# 00000
# output: 1
# 2
# input
# 11000
# 11000
# 00100
# 00011
# output: 3
def solution(grid: list):
def do_dfs(grid, i, j):
# check for all possible fails, ie boundary check
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[i]):
return
grid[i][j] = 0
do_dfs(grid, i + 1, j) # check up
do_dfs(grid, i - 1, j) # check down
do_dfs(grid, i, j - 1) # check left
do_dfs(grid, i, j + 1) # check right
# set an initial count up
count = 0
# loop through top left to bottom right
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 1:
count += 1
do_dfs(grid, i, j)
return count