-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0417-pacific-atlantic-water-flow.java
47 lines (39 loc) · 1.37 KB
/
0417-pacific-atlantic-water-flow.java
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
class Solution {
int[][] dir = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
public List<List<Integer>> pacificAtlantic(int[][] heights) {
List<List<Integer>> res = new ArrayList<>();
int rows = heights.length, cols = heights[0].length;
boolean[][] pacific = new boolean[rows][cols];
boolean[][] atlantic = new boolean[rows][cols];
for (int i = 0; i < cols; i++) {
dfs(heights, 0, i, Integer.MIN_VALUE, pacific);
dfs(heights, rows - 1, i, Integer.MIN_VALUE, atlantic);
}
for (int i = 0; i < rows; i++) {
dfs(heights, i, 0, Integer.MIN_VALUE, pacific);
dfs(heights, i, cols - 1, Integer.MIN_VALUE, atlantic);
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (pacific[i][j] && atlantic[i][j]) {
res.add(List.of(i, j));
}
}
}
return res;
}
private void dfs(
int[][] heights,
int i,
int j,
int prev,
boolean[][] ocean
) {
if (i < 0 || i >= ocean.length || j < 0 || j >= ocean[0].length) return;
if (heights[i][j] < prev || ocean[i][j]) return;
ocean[i][j] = true;
for (int[] d : dir) {
dfs(heights, i + d[0], j + d[1], heights[i][j], ocean);
}
}
}