forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShortest Bridge.py
90 lines (66 loc) · 2.43 KB
/
Shortest Bridge.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class Solution:
# O(I + W) Time | O(I + W) Space - Where "I" is the first island
# and "W" is the water to traverse.
def shortestBridge(self, grid: List[List[int]]) -> int:
start_row, start_col = self.__get_first_island(grid)
island_positions = self.__dfs(grid, start_row, start_col, [])
shortest_bridge = self.__bfs(grid, island_positions)
return shortest_bridge
# O(N) Time | O(1) Space
def __get_first_island(self, grid):
row_length = len(grid)
col_length = len(grid[0])
for row in range(row_length):
for col in range(col_length):
if grid[row][col] == 1:
return row, col
# O(N) Time | O(N) Space
def __dfs(self, grid, row, col, neighbors):
if row < 0 or row >= len(grid):
return
if col < 0 or col >= len(grid[0]):
return
if grid[row][col] != 1:
return
grid[row][col] = 2
neighbors.append((row, col))
self.__dfs(grid, row + 1, col, neighbors)
self.__dfs(grid, row - 1, col, neighbors)
self.__dfs(grid, row, col + 1, neighbors)
self.__dfs(grid, row, col - 1, neighbors)
return neighbors
# O(N) Time | O(N) Space
def __bfs(self, grid, queue):
steps = 0
while queue:
level = []
for _ in range(len(queue)):
curr_row, curr_col = queue.pop(0)
neighbors = self.__get_neighbors(grid, curr_row, curr_col)
for n_row, n_col in neighbors:
if grid[n_row][n_col] == 1:
return steps
elif grid[n_row][n_col] == 0:
grid[n_row][n_col] = -1 # Mark as visited
level.append((n_row, n_col))
queue = level
steps += 1
return steps
# O(1) Time | O(1) Space
def __get_neighbors(self, grid, row, col):
row_length = len(grid)
col_length = len(grid[0])
up = row - 1 >= 0
down = row + 1 < row_length
left = col - 1 >= 0
right = col + 1 < col_length
neighbors = []
if up:
neighbors.append((row - 1, col))
if down:
neighbors.append((row + 1, col))
if left:
neighbors.append((row, col - 1))
if right:
neighbors.append((row, col + 1))
return neighbors