Skip to content

Commit b573bd4

Browse files
committed
feat(leetcode): solve 417 - Pacific Atlantic Water Flow
1 parent 68e223b commit b573bd4

4 files changed

Lines changed: 141 additions & 22 deletions

File tree

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,37 @@ Automatically synchronize accepted LeetCode submissions to GitHub.
99
```text
1010
████████████░░░░░░░░░░░░░░░░░░
1111
12-
Solved: 123 / 300 Problems
12+
Solved: 124 / 300 Problems
1313
1414
📊 Statistics
1515
Difficulty Solved
1616
🟢 Easy 63
17-
🟡 Medium 54
17+
🟡 Medium 55
1818
🔴 Hard 6
19-
Total 123
19+
Total 124
2020
🔥 Latest Accepted Problem
2121
2222
2323
| Field | Value |
2424
|------|------|
25-
| Problem ID | 695 |
26-
| Title | Max Area Of Island |
25+
| Problem ID | 417 |
26+
| Title | Pacific Atlantic Water Flow |
2727
| Language | Python3 |
28-
| Runtime | 22 ms |
29-
| Memory | 20.7 MB |
28+
| Runtime | 27 ms |
29+
| Memory | 22.4 MB |
3030
3131
3232
💻 Languages Used
3333
34-
- **Python3** : 10
34+
- **Python3** : 11
3535
3636
## 📚 Recent Accepted Problems
3737
38+
- 417 Pacific Atlantic Water Flow
3839
- 695 Max Area Of Island
3940
- 695 Max Area Of Island
4041
- 695 Max Area Of Island
4142
- 695 Max Area Of Island
42-
- 200 Number Of Islands
4343
4444
4545
---
@@ -81,4 +81,4 @@ Git
8181
GitHub
8282
⏰ Last Sync
8383
84-
2026-07-05 13:37:57
84+
2026-07-05 19:17:09
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"submission_id": "2057018120",
3+
"question_id": "417",
4+
"title_slug": "pacific-atlantic-water-flow",
5+
"language": "python3",
6+
"language_verbose": "Python3",
7+
"runtime": "27 ms",
8+
"memory": "22.4 MB",
9+
"status_code": 10,
10+
"timestamp": 1783262686
11+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
class Solution:
2+
def pacificAtlantic(self, heights):
3+
4+
rows = len(heights)
5+
cols = len(heights[0])
6+
7+
pacific = set()
8+
atlantic = set()
9+
10+
def dfs(r, c, visited, prevHeight):
11+
12+
if (
13+
r < 0 or
14+
c < 0 or
15+
r >= rows or
16+
c >= cols
17+
):
18+
return
19+
20+
if (r, c) in visited:
21+
return
22+
23+
if heights[r][c] < prevHeight:
24+
return
25+
26+
visited.add((r, c))
27+
28+
dfs(r+1, c, visited, heights[r][c])
29+
dfs(r-1, c, visited, heights[r][c])
30+
dfs(r, c+1, visited, heights[r][c])
31+
dfs(r, c-1, visited, heights[r][c])
32+
33+
# Pacific
34+
for c in range(cols):
35+
dfs(0, c, pacific, heights[0][c])
36+
37+
for r in range(rows):
38+
dfs(r, 0, pacific, heights[r][0])
39+
40+
# Atlantic
41+
for c in range(cols):
42+
dfs(rows-1, c, atlantic, heights[rows-1][c])
43+
44+
for r in range(rows):
45+
dfs(r, cols-1, atlantic, heights[r][cols-1])
46+
47+
result = []
48+
49+
for r in range(rows):
50+
for c in range(cols):
51+
52+
if (r, c) in pacific and (r, c) in atlantic:
53+
result.append([r, c])
54+
55+
return result
56+
57+
rows = len(heights)
58+
cols = len(heights[0])
59+
60+
pacific = set()
61+
atlantic = set()
62+
63+
def dfs(r, c, visited, prevHeight):
64+
65+
if (
66+
r < 0 or
67+
c < 0 or
68+
r >= rows or
69+
c >= cols
70+
):
71+
return
72+
73+
if (r, c) in visited:
74+
return
75+
76+
if heights[r][c] < prevHeight:
77+
return
78+
79+
visited.add((r, c))
80+
81+
dfs(r+1, c, visited, heights[r][c])
82+
dfs(r-1, c, visited, heights[r][c])
83+
dfs(r, c+1, visited, heights[r][c])
84+
dfs(r, c-1, visited, heights[r][c])
85+
86+
# Pacific
87+
for c in range(cols):
88+
dfs(0, c, pacific, heights[0][c])
89+
90+
for r in range(rows):
91+
dfs(r, 0, pacific, heights[r][0])
92+
93+
# Atlantic
94+
for c in range(cols):
95+
dfs(rows-1, c, atlantic, heights[rows-1][c])
96+
97+
for r in range(rows):
98+
dfs(r, cols-1, atlantic, heights[r][cols-1])
99+
100+
result = []
101+
102+
for r in range(rows):
103+
for c in range(cols):
104+
105+
if (r, c) in pacific and (r, c) in atlantic:
106+
result.append([r, c])
107+
108+
return result

storage/stats.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"total": 123,
2+
"total": 124,
33
"easy": 63,
4-
"medium": 54,
4+
"medium": 55,
55
"hard": 6,
66
"languages": {
7-
"Python3": 10
7+
"Python3": 11
88
},
99
"recent_problems": [
1010
{
11-
"id": "695",
12-
"title": "Max Area Of Island"
11+
"id": "417",
12+
"title": "Pacific Atlantic Water Flow"
1313
},
1414
{
1515
"id": "695",
@@ -24,16 +24,16 @@
2424
"title": "Max Area Of Island"
2525
},
2626
{
27-
"id": "200",
28-
"title": "Number Of Islands"
27+
"id": "695",
28+
"title": "Max Area Of Island"
2929
}
3030
],
3131
"last_problem": {
32-
"id": "695",
33-
"title": "Max Area Of Island",
32+
"id": "417",
33+
"title": "Pacific Atlantic Water Flow",
3434
"language": "Python3",
35-
"runtime": "22 ms",
36-
"memory": "20.7 MB"
35+
"runtime": "27 ms",
36+
"memory": "22.4 MB"
3737
},
38-
"last_sync": "2026-07-05 13:37:57"
38+
"last_sync": "2026-07-05 19:17:09"
3939
}

0 commit comments

Comments
 (0)