LC.P417[太平洋大西洋水流问题]

题目描述

有一个 m × n 的矩形岛屿,与 太平洋大西洋 相邻。 “太平洋” 处于大陆的左边界和上边界,而 “大西洋” 处于大陆的右边界和下边界。

这个岛被分割成一个由若干方形单元格组成的网格。给定一个 m x n 的整数矩阵 heightsheights[r][c]表示坐标 (r, c) 上单元格 高于海平面的高度

岛上雨水较多,如果相邻单元格的高度 小于或等于 当前单元格的高度,雨水可以直接向北、南、东、西流向相邻单元格。水可以从海洋附近的任何单元格流入海洋。

返回网格坐标 result2D 列表 ,其中 result[i] = [ri, ci] 表示雨水从单元格 (ri, ci) 流动 既可流向太平洋也可流向大西洋

示例 1:

输入: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
输出: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]

示例 2:

输入: heights = [[2,1],[1,2]]
输出: [[0,0],[0,1],[1,0],[1,1]]

提示:

  • m == heights.length
  • n == heights[r].length
  • 1 <= m, n <= 200
  • 0 <= heights[r][c] <= 105

方法一:BFS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Solution {
int m, n;
int[][] grid;
int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

public List<List<Integer>> pacificAtlantic(int[][] heights) {
grid = heights;
m = grid.length;
n = grid[0].length;
Deque<int[]> q1 = new ArrayDeque<>(), q2 = new ArrayDeque<>();
boolean[][] res1 = new boolean[m][n], res2 = new boolean[m][n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (i == 0 || j == 0) {
res1[i][j] = true;
q1.offer(new int[]{i, j});
}
if (i == m - 1 || j == n - 1) {
res2[i][j] = true;
q2.offer(new int[]{i, j});
}
}
}
bfs(q1, res1);
bfs(q2, res2);
List<List<Integer>> ans = new ArrayList<>();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (res1[i][j] && res2[i][j]) {
List<Integer> list = new ArrayList<>();
list.add(i);
list.add(j);
ans.add(list);
}
}
}
return ans;
}

private void bfs(Deque<int[]> q, boolean[][] res) {
while (!q.isEmpty()) {
int[] poll = q.poll();
int x = poll[0], y = poll[1], t = grid[x][y];
for (int[] dir : dirs) {
int nx = x + dir[0], ny = y + dir[1];
if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue;
if (res[nx][ny] || grid[nx][ny] < t) continue;
q.offer(new int[]{nx, ny});
res[nx][ny] = true;
}
}
}
}
  • 时间复杂度:$O(m*n)$
  • 空间复杂度:$O(m*n)$

方法二:DFS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
int m, n;
int[][] grid;
int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

public List<List<Integer>> pacificAtlantic(int[][] heights) {
grid = heights;
m = grid.length;
n = grid[0].length;
boolean[][] res1 = new boolean[m][n], res2 = new boolean[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || j == 0) {
if (!res1[i][j]) dfs(i, j, res1);
}
if (i == m - 1 || j == n - 1) {
if (!res2[i][j]) dfs(i, j, res2);
}
}
}
List<List<Integer>> ans = new ArrayList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (res1[i][j] && res2[i][j]) {
List<Integer> list = new ArrayList<>();
list.add(i);
list.add(j);
ans.add(list);
}
}
}
return ans;
}

private void dfs(int x, int y, boolean[][] res) {
res[x][y] = true;
for (int[] dir : dirs) {
int nx = x + dir[0], ny = y + dir[1];
if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue;
if (res[nx][ny] || grid[nx][ny] < grid[x][y]) continue;
dfs(nx, ny, res);
}
}
}
  • 时间复杂度:$O(m*n)$
  • 空间复杂度:$O(m*n)$

方法三:并查集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Solution {
int m, n;
int[][] grid;
int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int N = 200 * 200 + 10;
int[] p1 = new int[N], p2 = new int[N];
int total, S, T;

private int find(int[] p, int x) {
if (p[x] != x) p[x] = find(p, p[x]);
return p[x];
}

private void union(int[] p, int a, int b) {
p[find(p, a)] = p[find(p, b)];
}

private boolean query(int[] p, int a, int b) {
return find(p, a) == find(p, b);
}

private int getIndex(int i, int j) {
return i * n + j;
}

public List<List<Integer>> pacificAtlantic(int[][] heights) {
grid = heights;
m = grid.length;
n = grid[0].length;
total = m * n;
S = total + 1;
T = total + 2;
for (int i = 0; i <= T; ++i) p1[i] = p2[i] = i;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int index = getIndex(i, j);
if (i == 0 || j == 0) {
if (!query(p1, S, index)) dfs(p1, S, i, j);
}
if (i == m - 1 || j == n - 1) {
if (!query(p2, T, index)) dfs(p2, T, i, j);
}
}
}
List<List<Integer>> ans = new ArrayList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int index = getIndex(i, j);
if (query(p1, S, index) && query(p2, T, index)) {
List<Integer> list = new ArrayList<>();
list.add(i);
list.add(j);
ans.add(list);
}
}
}
return ans;
}

private void dfs(int[] p, int o, int x, int y) {
union(p, o, getIndex(x, y));
for (int[] dir : dirs) {
int nx = x + dir[0], ny = y + dir[1];
if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue;
if (query(p, o, getIndex(nx, ny)) || grid[nx][ny] < grid[x][y]) continue;
dfs(p, o, nx, ny);
}
}
}
  • 时间复杂度:$O(m*n)$
  • 空间复杂度:$O(m*n)$