0%

Leetcode417-Pacific Atlantic Water Flow

Description

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:

  1. The order of returned grid coordinates does not matter.
  2. Both m and n are less than 150.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
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).

Solution

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
// Used two arrays to record if each cell can reach pacific or atlantic.
// Start from four sides to do DFS.

class Solution {
public List<List<Integer>> pacificAtlantic(int[][] matrix) {
List<List<Integer>> res = new ArrayList<>();
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return res;

int m = matrix.length;
int n = matrix[0].length;
boolean[][] pacific = new boolean[m][n];
boolean[][] altantic = new boolean[m][n];
for (int i = 0; i < m; i++){
helper(matrix, pacific, i, 0);
helper(matrix, altantic, i, n - 1);
}
for (int i = 0; i < n; i++){
helper(matrix, pacific, 0, i);
helper(matrix, altantic, m - 1, i);
}

for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++){
if (pacific[i][j] && altantic[i][j])
res.add(new ArrayList<Integer>(Arrays.asList(i, j)));
}

return res;
}

private int[][] dir = new int[][]{{-1, 0},{1, 0},{0, -1},{0, 1}};
private void helper(int[][] matrix, boolean[][] reachable, int x, int y){
reachable[x][y] = true;
for (int[] d: dir){
int nx = x + d[0];
int ny = y + d[1];
if (nx >=0 && nx < matrix.length && ny >= 0 && ny < matrix[0].length && !reachable[nx][ny] && matrix[nx][ny] >= matrix[x][y])
helper(matrix, reachable, nx, ny);
}
}
}