417 Pacific Atlantic Water Flow

417. Pacific Atlantic Water Flow

1. Question

Given anm x nmatrix 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:
  1. 1.
    The order of returned grid coordinates does not matter.
  2. 2.
    Both m and n are less than 150.
Example:
1
Given the following 5x5 matrix:
2
3
Pacific ~ ~ ~ ~ ~
4
~ 1 2 2 3 (5) *
5
~ 3 2 3 (4) (4) *
6
~ 2 4 (5) 3 1 *
7
~ (6) (7) 1 4 5 *
8
~ (5) 1 1 2 4 *
9
* * * * * Atlantic
10
11
Return:
12
[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).
Copied!

2. Implementation

(1) BFS
1
class Solution {
2
public List<int[]> pacificAtlantic(int[][] matrix) {
3
List<int[]> res = new ArrayList<>();
4
5
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
6
return res;
7
}
8
9
int m = matrix.length, n = matrix[0].length;
10
boolean[][] pVisited = new boolean[m][n];
11
boolean[][] aVisited = new boolean[m][n];
12
13
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
14
15
for (int i = 0; i < m; i++) {
16
searchByBFS(matrix, i, 0, pVisited, directions);
17
searchByBFS(matrix, i, n - 1, aVisited, directions);
18
}
19
20
for (int j = 0; j < n; j++) {
21
searchByBFS(matrix, 0, j, pVisited, directions);
22
searchByBFS(matrix, m - 1, j, aVisited, directions);
23
}
24
25
for (int i = 0; i < m; i++) {
26
for (int j = 0; j < n; j++) {
27
if (pVisited[i][j] && aVisited[i][j]) {
28
res.add(new int[] {i, j});
29
}
30
}
31
}
32
return res;
33
}
34
35
public void searchByBFS(int[][] matrix, int i, int j, boolean[][] visited, int[][] directions) {
36
visited[i][j] = true;
37
Queue<int[]> queue = new LinkedList<>();
38
queue.add(new int[] {i, j});
39
40
while (!queue.isEmpty()) {
41
int[] curCell = queue.remove();
42
int curRow = curCell[0], curCol = curCell[1];
43
44
for (int[] direction : directions) {
45
int nextRow = curRow + direction[0];
46
int nextCol = curCol + direction[1];
47
48
if (isValid(matrix, curRow, curCol, nextRow, nextCol, visited)) {
49
visited[nextRow][nextCol] = true;
50
queue.add(new int[] {nextRow, nextCol});
51
}
52
}
53
}
54
}
55
56
public boolean isValid(int[][] matrix, int curRow, int curCol, int nextRow, int nextCol, boolean[][] visited) {
57
return nextRow >= 0 && nextRow < matrix.length && nextCol >= 0 && nextCol < matrix[0].length && !visited[nextRow][nextCol] && matrix[curRow][curCol] <= matrix[nextRow][nextCol];
58
}
59
}
Copied!
(2) DFS
1
public class Solution {
2
public List<int[]> pacificAtlantic(int[][] matrix) {
3
List<int[]> res = new ArrayList<>();
4
5
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
6
return res;
7
}
8
9
int m = matrix.length, n = matrix[0].length;
10
boolean[][] aVisited = new boolean[m][n];
11
boolean[][] pVisited = new boolean[m][n];
12
13
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
14
15
for (int i = 0; i < m; i++) {
16
searchByDFS(i, 0, matrix, directions, pVisited);
17
searchByDFS(i, n - 1, matrix, directions, aVisited);
18
}
19
20
for (int j = 0; j < n; j++) {
21
searchByDFS(0, j, matrix, directions, pVisited);
22
searchByDFS(m - 1, j, matrix, directions, aVisited);
23
}
24
25
for (int i = 0; i < m; i++) {
26
for (int j = 0; j < n; j++) {
27
if (pVisited[i][j] && aVisited[i][j]) {
28
res.add(new int[] {i, j});
29
}
30
}
31
}
32
return res;
33
}
34
35
public void searchByDFS(int row, int col, int[][] matrix, int[][] directions, boolean[][] visited) {
36
visited[row][col] = true;
37
38
for (int[] direction : directions) {
39
int nextRow = row + direction[0];
40
int nextCol = col + direction[1];
41
42
if (isValid(row, col, nextRow, nextCol, matrix, visited)) {
43
searchByDFS(nextRow, nextCol, matrix, directions, visited);
44
}
45
}
46
}
47
48
public boolean isValid(int curRow, int curCol, int nextRow, int nextCol, int[][] matrix, boolean[][] visited) {
49
return nextRow >= 0 && nextRow < matrix.length && nextCol >= 0 && nextCol < matrix[0].length && !visited[nextRow][nextCol] && matrix[curRow][curCol] <= matrix[nextRow][nextCol];
50
}
51
}
Copied!

3. Time & Space Complexity

BFS: 时间复杂度O(mn) , 空间复杂度:O(mn)
DFS: 时间复杂度O(mn) , 空间复杂度:O(mn)