-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path417.java
73 lines (68 loc) · 2.31 KB
/
417.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
class Solution {
// Keeps track of cells we have already visited.
boolean[][] visited_pac;
boolean[][] visited_atl;
public List<List<Integer>> pacificAtlantic(int[][] heights) {
visited_pac = new boolean[heights.length][heights[0].length];
visited_atl = new boolean[heights.length][heights[0].length];
// Do BFS from pacific ocean, then again from Atlantic ocean.
Queue<int[]> q = new LinkedList<>();
for (int i = 0; i < heights.length; i++) {
q.add(new int[] { i, 0 });
visited_pac[i][0] = true;
}
for (int i = 1; i < heights[0].length; i++) {
q.add(new int[] { 0, i });
visited_pac[0][i] = true;
}
int[][] directions = new int[][] { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
while (!q.isEmpty()) {
int[] cur = q.poll();
for (int[] dir : directions) {
int i = dir[0] + cur[0];
int j = dir[1] + cur[1];
if (i >= 0 && i < heights.length && j >= 0 && j < heights[0].length && !visited_pac[i][j]
&& heights[cur[0]][cur[1]] <= heights[i][j]) {
visited_pac[i][j] = true;
q.add(new int[] { i, j });
}
}
}
for (int i = 0; i < heights.length; i++) {
q.add(new int[] { i, heights[0].length - 1 });
visited_atl[i][heights[0].length - 1] = true;
}
for (int i = 0; i < heights[0].length; i++) {
q.add(new int[] { heights.length - 1, i });
visited_atl[heights.length - 1][i] = true;
}
while (!q.isEmpty()) {
int[] cur = q.poll();
for (int[] dir : directions) {
int i = dir[0] + cur[0];
int j = dir[1] + cur[1];
if (i >= 0 && i < heights.length && j >= 0 && j < heights[0].length && !visited_atl[i][j]
&& heights[cur[0]][cur[1]] <= heights[i][j]) {
visited_atl[i][j] = true;
q.add(new int[] { i, j });
}
}
}
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < heights.length; i++) {
for (int j = 0; j < heights[0].length; j++) {
if (visited_atl[i][j] && visited_pac[i][j]) {
List<Integer> cell = new ArrayList<>();
cell.add(i);
cell.add(j);
res.add(cell);
}
}
}
return res;
}
}