-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
69 lines (57 loc) · 1.9 KB
/
Solution.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
"""
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
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
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).
"""
def dfs(matrix, memo, x, y):
m, n = len(matrix), len(matrix[0])
memo[x][y] = True
stack = [[x, y]]
while stack:
x, y = stack.pop()
memo[x][y] = True
for a, b in [[-1,0],[1,0],[0,-1],[0,1]]:
if x + a < 0 or x + a >= m or y + b < 0 or y + b >= n or memo[x + a][y + b] or matrix[x + a][y + b] < matrix[x][y]: # out of range of visited
continue
stack.append([x + a, y + b])
def Solution(matrix):
m, n = len(matrix), len(matrix[0])
pacific = [[False for _ in range(n)] for _ in range(m)]
atlantic = [[False for _ in range(n)] for _ in range(m)]
for i in range(m):
dfs(matrix, atlantic, i, n - 1)
dfs(matrix, pacific, i, 0)
for j in range(n):
dfs(matrix, atlantic, n - 1, j)
dfs(matrix, pacific, 0, j)
res = []
for i in range(m):
for j in range(n):
if pacific[i][j] and atlantic[i][j]:
res.append([i,j])
print(res)
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]]
Solution(matrix)
"""
[(0,0)]
[(0,1),(1,0)], [(0,1),(1,1),(2,0)], [(0,1),(1,1),(2,1),(3,0)], [(0,1),(1,1),(2,1),()
"""