-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet-code-q-40.java
More file actions
49 lines (41 loc) · 1.63 KB
/
leet-code-q-40.java
File metadata and controls
49 lines (41 loc) · 1.63 KB
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
class Solution {
public List<List<Integer>> pacificAtlantic(int[][] heights) {
int m = heights.length;
int n = heights[0].length;
boolean[][] pacific = new boolean[m][n];
boolean[][] atlantic = new boolean[m][n];
// DFS from Pacific (top & left) and Atlantic (bottom & right) borders
for (int i = 0; i < m; i++) {
dfs(heights, pacific, i, 0, heights[i][0]);
dfs(heights, atlantic, i, n - 1, heights[i][n - 1]);
}
for (int j = 0; j < n; j++) {
dfs(heights, pacific, 0, j, heights[0][j]);
dfs(heights, atlantic, m - 1, j, heights[m - 1][j]);
}
// Collect cells reachable by both oceans
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (pacific[i][j] && atlantic[i][j]) {
result.add(Arrays.asList(i, j));
}
}
}
return result;
}
private void dfs(int[][] heights, boolean[][] visited, int r, int c, int prevHeight) {
int m = heights.length;
int n = heights[0].length;
// Base cases
if (r < 0 || r >= m || c < 0 || c >= n) return;
if (visited[r][c]) return;
if (heights[r][c] < prevHeight) return;
visited[r][c] = true;
// Explore in 4 directions
dfs(heights, visited, r + 1, c, heights[r][c]);
dfs(heights, visited, r - 1, c, heights[r][c]);
dfs(heights, visited, r, c + 1, heights[r][c]);
dfs(heights, visited, r, c - 1, heights[r][c]);
}
}