Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1f21d58

Browse filesBrowse files
author
night
committed
dfs
1 parent 1f2e4bd commit 1f21d58
Copy full SHA for 1f21d58

File tree

2 files changed

+121
-0
lines changed
Filter options

2 files changed

+121
-0
lines changed

‎note/417/README.md

Copy file name to clipboard
+69Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# [Pacific Atlantic Water Flow][title]
2+
3+
## Solution
4+
因为边界的单元格可以直接流入到海洋中, 可以分别以左边界和上边界进行深搜计算可能流入到太平洋的内部单元格, 右边界和下边界进行深搜计算可能流入到大西洋的单元格。
5+
因为是反向遍历,原先从内部遍历是 <= 的情况, 我们从边界逆向深搜时要改为 >=。
6+
7+
```kotlin
8+
class Solution {
9+
10+
lateinit var heights: Array<IntArray>
11+
12+
fun pacificAtlantic(heights: Array<IntArray>): List<List<Int>> {
13+
if (heights.isEmpty()) return emptyList()
14+
this.heights = heights
15+
val result = mutableListOf<List<Int>>()
16+
val m = heights.size
17+
val n = heights.first().size
18+
val pacific = Array(m) { BooleanArray(n) }
19+
val atlantic = Array(m) { BooleanArray(n) }
20+
for (i in heights.indices) {
21+
dfs(i, 0, pacific)
22+
dfs(i, n - 1, atlantic)
23+
}
24+
for (i in heights.first().indices) {
25+
dfs(0, i, pacific)
26+
dfs(m - 1, i, atlantic)
27+
}
28+
for (i in heights.indices) {
29+
for (j in heights.first().indices) {
30+
if (pacific[i][j] && atlantic[i][j]) {
31+
result.add(listOf(i, j))
32+
}
33+
}
34+
}
35+
return result
36+
}
37+
38+
private val dirs = arrayOf(
39+
intArrayOf(0, 1),
40+
intArrayOf(0, -1),
41+
intArrayOf(-1, 0),
42+
intArrayOf(1, 0),
43+
)
44+
45+
private fun dfs(row: Int, col: Int, ocean: Array<BooleanArray>) {
46+
if (ocean[row][col]) return
47+
ocean[row][col] = true
48+
for ((rowDiff, colDiff) in dirs) {
49+
val newRow = row + rowDiff
50+
val newCol = col + colDiff
51+
if (newRow in ocean.indices && newCol in ocean.first().indices && heights[newRow][newCol] >= heights[row][col]) {
52+
dfs(newRow, newCol, ocean)
53+
}
54+
}
55+
}
56+
}
57+
58+
```
59+
60+
61+
62+
## Conclusion
63+
64+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-kotlin-leetcode][akl]
65+
66+
67+
68+
[title]: https://leetcode.cn/problems/pacific-atlantic-water-flow/description/
69+
[akl]: https://github.com/NightXlt/awesome-kotlin-leetcode
+52Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.blankj.google
2+
3+
// T: O(M*N) S: O(M*N)
4+
class PacificAtlantic {
5+
6+
7+
lateinit var heights: Array<IntArray>
8+
fun pacificAtlantic(heights: Array<IntArray>): List<List<Int>> {
9+
if (heights.isEmpty()) return emptyList()
10+
this.heights = heights
11+
val result = mutableListOf<List<Int>>()
12+
val m = heights.size
13+
val n = heights.first().size
14+
val pacific = Array(m) { BooleanArray(n) }
15+
val atlantic = Array(m) { BooleanArray(n) }
16+
for (i in heights.indices) {
17+
dfs(i, 0, pacific)
18+
dfs(i, n - 1, atlantic)
19+
}
20+
for (i in heights.first().indices) {
21+
dfs(0, i, pacific)
22+
dfs(m - 1, i, atlantic)
23+
}
24+
for (i in heights.indices) {
25+
for (j in heights.first().indices) {
26+
if (pacific[i][j] && atlantic[i][j]) {
27+
result.add(listOf(i, j))
28+
}
29+
}
30+
}
31+
return result
32+
}
33+
34+
private val dirs = arrayOf(
35+
intArrayOf(0, 1),
36+
intArrayOf(0, -1),
37+
intArrayOf(-1, 0),
38+
intArrayOf(1, 0),
39+
)
40+
41+
private fun dfs(row: Int, col: Int, ocean: Array<BooleanArray>) {
42+
if (ocean[row][col]) return
43+
ocean[row][col] = true
44+
for ((rowDiff, colDiff) in dirs) {
45+
val newRow = row + rowDiff
46+
val newCol = col + colDiff
47+
if (newRow in ocean.indices && newCol in ocean.first().indices && heights[newRow][newCol] >= heights[row][col]) {
48+
dfs(newRow, newCol, ocean)
49+
}
50+
}
51+
}
52+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.