-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path417_PacificAtlanticWaterFlow417.java
107 lines (101 loc) · 3.44 KB
/
417_PacificAtlanticWaterFlow417.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Given an m x n matrix of non-negative integers representing the height of
* each unit cell in a continent, the "Pacific ocean" touches the left and top
* edges of the matrix and the "Atlantic ocean" touches the right and bottom
* edges.
*
* Water can only flow in four directions (up, down, left, or right) from a
* cell to another one with height equal or lower.
*
* Find the list of grid coordinates where water can flow to both the Pacific
* and Atlantic ocean.
*
* Note:
* The order of returned grid coordinates does not matter.
* Both m and n are less than 150.
*
* Example:
*
* Given the following 5x5 matrix:
*
* Pacific ~ ~ ~ ~ ~
* ~ 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 *
* * * * * * Atlantic
*
* Return:
*
* [[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with
* parentheses in above matrix).
*/
public class PacificAtlanticWaterFlow417 {
private int[][] dirs = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public List<int[]> pacificAtlantic(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return new ArrayList<>();
int M = matrix.length;
int N = matrix[0].length;
boolean[][] pacific = new boolean[M][N];
boolean[][] visited = new boolean[M][N];
Queue<int[]> q = new LinkedList<>();
for (int j=0; j<N; j++) {
q.add(new int[]{0, j});
pacific[0][j] = true;
visited[0][j] = true;
}
for (int i=1; i<M; i++) {
q.add(new int[]{i, 0});
pacific[i][0] = true;
visited[i][0] = true;
}
while (!q.isEmpty()) {
int[] now = q.poll();
for (int[] d: dirs) {
int x = now[0] + d[0];
int y = now[1] + d[1];
if (x >= 0 && y >= 0 && x < M && y < N && !visited[x][y] && matrix[x][y] >= matrix[now[0]][now[1]]) {
q.add(new int[]{x, y});
pacific[x][y] = true;
visited[x][y] = true;
}
}
}
boolean[][] atlantic = new boolean[M][N];
visited = new boolean[M][N];
q = new LinkedList<>();
for (int j=0; j<N; j++) {
q.add(new int[]{M-1, j});
atlantic[M-1][j] = true;
visited[M-1][j] = true;
}
for (int i=0; i<M-1; i++) {
q.add(new int[]{i, N-1});
atlantic[i][N-1] = true;
visited[i][N-1] = true;
}
while (!q.isEmpty()) {
int[] now = q.poll();
for (int[] d: dirs) {
int x = now[0] + d[0];
int y = now[1] + d[1];
if (x >= 0 && y >= 0 && x < M && y < N && !visited[x][y] && matrix[x][y] >= matrix[now[0]][now[1]]) {
q.add(new int[]{x, y});
atlantic[x][y] = true;
visited[x][y] = true;
}
}
}
List<int[]> res = new ArrayList<>();
for (int i=0; i<M; i++) {
for (int j=0; j<N; j++) {
if (atlantic[i][j] && pacific[i][j]) {
// System.out.println(i + ", " + j);
res.add(new int[]{i, j});
}
}
}
return res;
}
}