mirror of
https://github.com/prdlk/leetcode.git
synced 2026-11-02 21:22:43 +00:00
docs(docs): remove array order meta and add solution implementation for Pacific Atlantic Water Flow
This commit is contained in:
1 parent
d1c99c307e
commit
f5f31ba1c3
2 files changed
+35
-1
No files matched your search
@@ -1,3 +1,3 @@
|
|||||||
import { defineMeta } from "blume";
|
import { defineMeta } from "blume";
|
||||||
|
|
||||||
export default defineMeta({ title: "Array", order: 0 });
|
export default defineMeta({ title: "Array" });
|
||||||
@@ -30,4 +30,38 @@ sidebar:
|
|||||||
```py
|
```py
|
||||||
class Solution:
|
class Solution:
|
||||||
def pacificAtlantic(self, heights: list[list[int]]) -> list[list[int]]:
|
def pacificAtlantic(self, heights: list[list[int]]) -> list[list[int]]:
|
||||||
|
rows, cols = len(heights), len(heights[0])
|
||||||
|
pac, atl = set(), set()
|
||||||
|
|
||||||
|
def dfs(r, c, seen, prevHeight):
|
||||||
|
if (
|
||||||
|
(r, c) in seen
|
||||||
|
or r < 0
|
||||||
|
or c < 0
|
||||||
|
or r == rows
|
||||||
|
or c == cols
|
||||||
|
or heights[r][c] < prevHeight
|
||||||
|
):
|
||||||
|
return
|
||||||
|
seen.add((r, c))
|
||||||
|
dfs(r + 1, c, seen, heights[r][c])
|
||||||
|
dfs(r - 1, c, seen, heights[r][c])
|
||||||
|
dfs(r, c + 1, seen, heights[r][c])
|
||||||
|
dfs(r, c - 1, seen, heights[r][c])
|
||||||
|
|
||||||
|
for c in range(cols):
|
||||||
|
dfs(0, c, pac, heights[0][c])
|
||||||
|
dfs(rows - 1, c, atl, heights[rows - 1][c])
|
||||||
|
|
||||||
|
for r in range(rows):
|
||||||
|
dfs(r, 0, pac, heights[r][0])
|
||||||
|
dfs(r, cols - 1, atl, heights[r][cols - 1])
|
||||||
|
|
||||||
|
res = []
|
||||||
|
for r in range(rows):
|
||||||
|
for c in range(cols):
|
||||||
|
if (r, c) in pac and (r, c) in atl:
|
||||||
|
res.append([r, c])
|
||||||
|
|
||||||
|
return res
|
||||||
```
|
```
|
||||||
Reference in new issue
Block a user