-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0417_pacific_atlantic_water_flow.py
61 lines (55 loc) · 2.45 KB
/
0417_pacific_atlantic_water_flow.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
from typing import List
import collections
class Solution:
def bfs(self, matrix, row, col, ocean):
visited = set()
queue = [(row, col)]
ocean[row][col] = 1
rows, cols = len(matrix), len(matrix[0])
directions = ((0, 1), (0, -1), (1, 0), (-1, 0))
# 1 While queue not empty
while queue:
curr_r, curr_c = queue.pop(0)
prev_val = matrix[curr_r][curr_c]
if (curr_r, curr_c) not in visited:
visited.add((curr_r, curr_c))
# Neighbors
for direction in directions:
next_r, next_c = curr_r + direction[0], curr_c + direction[1]
try:
# 2 Check necessary conditions to add in queue
if 0 <= next_r < rows and 0 <= next_c < cols and matrix[next_r][next_c] >= prev_val:
queue.append((next_r, next_c))
ocean[next_r][next_c] = 1
except Exception as ex:
print(row)
print(col)
print(ex)
def pacificAtlantic(self, matrix: List[List[int]]) -> List[List[int]]:
if not matrix or not matrix[0]:
return []
rows, cols = len(matrix), len(matrix[0])
# 1 create two matrix one for Pacific one for Atlantic
atlantic = [[0 for x in range(cols)] for y in range(rows)]
pacific = [[0 for x in range(cols)] for y in range(rows)]
# 2 Start from oceans and follow the flow from ocean to top
# Loop top and bottom
for col in range(len(matrix[0])):
#pacific[0][col]=atlantic[rows-1][0] = 1
self.bfs(matrix, 0, col, pacific)
self.bfs(matrix, rows-1, col, atlantic)
# Loop left and right
for row in range(len(matrix)):
#pacific[row][0]=atlantic[row][cols-1] = 1
self.bfs(matrix, row, 0, pacific)
self.bfs(matrix, row, cols-1, atlantic)
res = []
# 3 Find the intersection of the two solutions groups and create the final solution
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if( atlantic[i][j] == 1 and pacific[i][j] == 1 ):
res.append((i,j))
return res
sol = Solution()
matrix = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
print(sol.pacificAtlantic(matrix))