-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLC417-Pacific-Atlantic-Water-Flow.py
83 lines (69 loc) · 2.85 KB
/
LC417-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
"""
Given an m x n matrix of non-negative integers
representing the height of each unit cell in a
continent, the "Pacific ocean" touches the left
and top edges of the matrix and the "Atlantic
ocean" touches the right and bottom edges.
Water can only flow in four directions (up, down,
left, or right) from a cell to another one with
height equal or lower.
Find the list of grid coordinates where water
can flow to both the Pacific and Atlantic ocean.
Note:
The order of returned grid coordinates does not matter.
Both m and n are less than 150.
Example:
Given the following 5x5 matrix:
Pacific ~ ~ ~ ~ ~
~ 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 *
* * * * * Atlantic
Return:
[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]]
(positions with parentheses in above matrix).
"""
from typing import List
class Solution:
def pacificAtlantic(self, matrix: List[List[int]]) -> List[List[int]]:
if not matrix or not matrix[0]:
return []
self.n_rows = len(matrix)
self.n_cols = len(matrix[0])
self.matrix = matrix
reaches_pacific = self.get_cluster(
[(0, n) for n in range(self.n_cols)] + [(n, 0) for n in range(1, self.n_rows)])
reaches_atlantic = self.get_cluster(
[(self.n_rows - 1, n) for n in range(self.n_cols)] + [(n, self.n_cols - 1) for n in range(self.n_rows)])
ans = sorted(list(reaches_pacific & reaches_atlantic))
return [list(p) for p in ans]
def get_cluster(self, starting: List[List[int]]) -> set:
"""
get all the cells from which water can reach one of starting cells
"""
cur = starting
reaches = set(starting)
while cur:
x, y = cur.pop()
if x > 0 and (x - 1, y) not in reaches and self.matrix[x - 1][y] >= self.matrix[x][y]:
cur.append((x - 1, y))
reaches.add((x - 1, y))
if x + 1 < self.n_rows and (x + 1, y) not in reaches and self.matrix[x + 1][y] >= self.matrix[x][y]:
cur.append((x + 1, y))
reaches.add((x + 1, y))
if y > 0 and (x, y - 1) not in reaches and self.matrix[x][y - 1] >= self.matrix[x][y]:
cur.append((x, y - 1))
reaches.add((x, y - 1))
if y + 1 < self.n_cols and (x, y + 1) not in reaches and self.matrix[x][y + 1] >= self.matrix[x][y]:
cur.append((x, y + 1))
reaches.add((x, y + 1))
return reaches
if __name__ == '__main__':
import run_tests
correct_answers = [
[[[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]],
[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]]]
]
run_tests.run_tests(Solution().pacificAtlantic, correct_answers)