-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacific_atlantic_water_flow.py
100 lines (74 loc) · 3.44 KB
/
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
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
91
92
93
94
95
96
97
98
99
100
"""
There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges.
The island is partitioned into a grid of square cells. You are given an m x n integer matrix heights where heights[r][c] represents the height above sea level of the cell at coordinate (r, c).
The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is less than or equal to the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean.
Return a 2D list of grid coordinates result where result[i] = [ri, ci] denotes that rain water can flow from cell (ri, ci) to both the Pacific and Atlantic oceans.
Example 1:
Input: heights = [[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]]
Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below:
[0,4]: [0,4] -> Pacific Ocean
[0,4] -> Atlantic Ocean
[1,3]: [1,3] -> [0,3] -> Pacific Ocean
[1,3] -> [1,4] -> Atlantic Ocean
[1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean
[1,4] -> Atlantic Ocean
[2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean
[2,2] -> [2,3] -> [2,4] -> Atlantic Ocean
[3,0]: [3,0] -> Pacific Ocean
[3,0] -> [4,0] -> Atlantic Ocean
[3,1]: [3,1] -> [3,0] -> Pacific Ocean
[3,1] -> [4,1] -> Atlantic Ocean
[4,0]: [4,0] -> Pacific Ocean
[4,0] -> Atlantic Ocean
Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans.
Example 2:
Input: heights = [[1]]
Output: [[0,0]]
Explanation: The water can flow from the only cell to the Pacific and Atlantic oceans.
Constraints:
m == heights.length
n == heights[r].length
1 <= m, n <= 200
0 <= heights[r][c] <= 105
"""
from collections import deque
class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
p_que = deque()
p_seen = set()
a_que = deque()
a_seen = set()
m = len(heights) # rows
n = len(heights[0]) # cols
# top row
for j in range(n):
p_que.append((0, j))
p_seen.add((0, j))
# left column excluding top left corner
for i in range(1, m):
p_que.append((i, 0))
p_seen.add((i, 0))
# right wall including bottom right corner
for i in range(m):
a_que.append((i, n-1))
a_seen.add((i, n-1))
# bottom row excluding that right corner
for j in range(n-1):
a_que.append((m-1, j))
a_seen.add((m-1, j))
# BFS Function
def get_coords(que, seen):
coords = set()
while que:
i, j = que.popleft()
coords.add((i, j))
for i_off, j_off in [(0, 1), (1,0), (-1, 0), (0,-1)]:
r, c = i + i_off, j + j_off
if 0 <= r < m and 0<=c< n and heights[r][c] >= heights[i][j] and (r, c) not in seen:
seen.add((r, c))
que.append((r, c))
return coords
p_coords = get_coords(p_que, p_seen)
a_coords = get_coords(a_que, a_seen)
return list(p_coords.intersection(a_coords))