forked from fullstackmeetups/coding-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
26 lines (22 loc) · 671 Bytes
/
Copy pathscript.py
File metadata and controls
26 lines (22 loc) · 671 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
def find_island(given_matrix):
rows, cols = len(given_matrix), len(given_matrix[0])
def dfs(row, col):
if row < 0 or col < 0 or row == rows or col == cols:
return 0
if given_matrix[row][col] == 0:
return 0
given_matrix[row][col] = 0
dfs(row - 1, col)
dfs(row + 1, col)
dfs(row, col - 1)
dfs(row, col + 1)
return 1
res = 0
for r in range(rows):
for c in range(cols):
res += dfs(r, c)
return res
print(find_island([[1, 1, 0, 0, 0],
[1, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 1]]))