Skip to content

Commit 7d306a0

Browse files
Create 0417-pacific-atlantic-water-flow.go
Accepted submission: _https://leetcode.com/submissions/detail/870855223/_
1 parent 7850b58 commit 7d306a0

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
func pacificAtlantic(heights [][]int) [][]int {
2+
ROWS, COLS := len(heights), len(heights[0])
3+
pac, atl := make(map[int]bool), make(map[int] bool)
4+
5+
var dfs func(int, int, map[int]bool, int)
6+
dfs = func(r, c int, visit map[int]bool, prevHeight int) {
7+
if (
8+
visit[r*COLS + c] ||
9+
r < 0 ||
10+
c < 0 ||
11+
r == ROWS ||
12+
c == COLS ||
13+
heights[r][c] < prevHeight) {
14+
return;
15+
}
16+
visit[r*COLS + c] = true
17+
dfs(r + 1, c, visit, heights[r][c])
18+
dfs(r - 1, c, visit, heights[r][c])
19+
dfs(r, c + 1, visit, heights[r][c])
20+
dfs(r, c - 1, visit, heights[r][c])
21+
}
22+
23+
for c := 0; c < COLS; c++ {
24+
dfs(0, c, pac, heights[0][c])
25+
dfs(ROWS - 1, c, atl, heights[ROWS - 1][c])
26+
}
27+
28+
for r := 0; r < ROWS; r++ {
29+
dfs(r, 0, pac, heights[r][0])
30+
dfs(r, COLS - 1, atl, heights[r][COLS - 1])
31+
}
32+
33+
res := make([][]int, 0)
34+
for r := 0; r < ROWS; r++ {
35+
for c := 0; c < COLS; c++ {
36+
if pac[r*COLS + c] && atl[r*COLS + c] {
37+
res = append(res, []int{r, c})
38+
}
39+
}
40+
}
41+
return res
42+
}

0 commit comments

Comments
 (0)