Skip to content

Commit 442fe0b

Browse files
authored
Update Pacific Atlantic Water Flow - Leetcode 417.py
1 parent e186410 commit 442fe0b

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

Pacific Atlantic Water Flow - Leetcode 417.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,25 @@
22

33
class Solution:
44
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
5+
# Note: If you watched the video for this, you would have seen slightly
6+
# different code that uses another set called "coords". You don't need this,
7+
# because we can just use the intersection of a_seen and p_seen instead!
8+
59
p_que = deque()
610
p_seen = set()
7-
811
a_que = deque()
912
a_seen = set()
10-
1113
m, n = len(heights), len(heights[0])
1214

1315
for j in range(n):
1416
p_que.append((0, j))
1517
p_seen.add((0, j))
16-
1718
for i in range(1, m):
1819
p_que.append((i, 0))
1920
p_seen.add((i, 0))
20-
2121
for i in range(m):
2222
a_que.append((i, n - 1))
2323
a_seen.add((i, n - 1))
24-
2524
for j in range(n - 1):
2625
a_que.append((m - 1, j))
2726
a_seen.add((m - 1, j))
@@ -30,14 +29,12 @@ def get_coords(que, seen):
3029
coords = set()
3130
while que:
3231
i, j = que.popleft()
33-
coords.add((i, j))
3432
for i_off, j_off in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
3533
r, c = i + i_off, j + j_off
3634
if 0 <= r < m and 0 <= c < n and heights[r][c] >= heights[i][j] and (r, c) not in seen:
3735
seen.add((r, c))
3836
que.append((r, c))
39-
return coords
40-
41-
p_coords = get_coords(p_que, p_seen)
42-
a_coords = get_coords(a_que, a_seen)
43-
return list(p_coords.intersection(a_coords))
37+
38+
get_coords(p_que, p_seen)
39+
get_coords(a_que, a_seen)
40+
return list(p_seen.intersection(a_seen))

0 commit comments

Comments
 (0)