-
Notifications
You must be signed in to change notification settings - Fork 1
/
number-of-islands.py
71 lines (59 loc) · 2.21 KB
/
number-of-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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Leetcode 200. Number of Islands
#
# Link: https://leetcode.com/problems/number-of-islands/
# Difficulty: Medium
# Solution using BFS and visited set.
# Complexity:
# O(M*N) time | where M and N represent the rows and cols of the input matrix
# O(M*N) space | where M and N represent the rows and cols of the input matrix
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
ROWS, COLS = len(grid), len(grid[0])
DIRECTIONS = ((0,1), (0,-1), (1,0), (-1,0))
count = 0
visited = set()
def bfs(r, c):
q = deque()
q.append((r,c))
while q:
r, c = q.popleft()
for dr, dc in DIRECTIONS:
newR, newC = r + dr, c + dc
if (newR in range(ROWS) and
newC in range(COLS) and
(newR, newC) not in visited):
visited.add((newR, newC))
if grid[newR][newC] == "1":
q.append((newR, newC))
for r in range(ROWS):
for c in range(COLS):
if (r,c) not in visited and grid[r][c] == "1":
count += 1
bfs(r, c)
return count
# Solution using DFS and changing grid values instead of visited set.
# Complexity:
# O(M*N) time | where M and N represent the rows and cols of the input matrix
# O(1) space
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
ROWS, COLS = len(grid), len(grid[0])
DIRECTIONS = ((0,1), (0,-1), (1,0), (-1,0))
count = 0
def dfs(r, c):
stack = [(r, c)]
while stack:
r, c = stack.pop()
grid[r][c] = '0'
for dr, dc in DIRECTIONS:
newR, newC = r + dr, c + dc
if (newR in range(ROWS) and
newC in range(COLS) and
grid[newR][newC] == '1'):
stack.append((newR, newC))
for r in range(ROWS):
for c in range(COLS):
if grid[r][c] == '1':
dfs(r, c)
count += 1
return count