Leetcode: Pacific Atlantic Water Flow

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).

这题考点在于需要设置两个visited数组

Two Queue and add all the Pacific border to one queue; Atlantic border to another queue.

Keep a visited matrix for each queue. In the end, add the cell visited by two queue to the result.
BFS: Water flood from ocean to the cell. Since water can only flow from high/equal cell to low cell, add the neighboor cell with height larger or equal to current cell to the queue and mark as visited.(逆流而上)

Solution 1: 我自己的DFS (beat 89%)

 1 public class Solution {
 2     int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
 3     public List<int[]> pacificAtlantic(int[][] matrix) {
 4         List<int[]> res = new ArrayList<int[]>();
 5         if (matrix==null || matrix.length==0 || matrix[0].length==0) return res;
 6         int n = matrix.length, m = matrix[0].length;
 7         boolean[][] pVisited = new boolean[n][m];
 8         boolean[][] aVisited = new boolean[n][m];
 9         for (int i=0; i<n; i++) {
10             //pacific
11             dfs(matrix, i, 0, pVisited);
12             //atlatic
13             dfs(matrix, i, m-1, aVisited);
14         }
15
16         for (int j=0; j<m; j++) {
17             //pacific
18             dfs(matrix, 0, j, pVisited);
19             //atlatic
20             dfs(matrix, n-1, j, aVisited);
21         }
22
23         for (int i=0; i<n; i++) {
24             for (int j=0; j<m; j++) {
25                 if (pVisited[i][j] && aVisited[i][j])
26                     res.add(new int[]{i, j});
27             }
28         }
29         return res;
30     }
31
32     public void dfs(int[][] matrix, int i, int j, boolean[][] visited) {
33         int n = matrix.length, m = matrix[0].length;
34         visited[i][j] = true;
35         for (int[] dir : directions) {
36             int row = dir[0] + i;
37             int col = dir[1] + j;
38             if (row>=0 && row<n && col>=0 && col<m && !visited[row][col] && matrix[i][j]<=matrix[row][col])
39                 dfs(matrix, row, col, visited);
40         }
41     }
42 }

Solution 2: BFS, refer to https://discuss.leetcode.com/topic/62379/java-bfs-dfs-from-ocean/2

 1 public class Solution {
 2     int[][]dir = new int[][]{{1,0},{-1,0},{0,1},{0,-1}};
 3     public List<int[]> pacificAtlantic(int[][] matrix) {
 4         List<int[]> res = new LinkedList<>();
 5         if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
 6             return res;
 7         }
 8         int n = matrix.length, m = matrix[0].length;
 9         //One visited map for each ocean
10         boolean[][] pacific = new boolean[n][m];
11         boolean[][] atlantic = new boolean[n][m];
12         Queue<int[]> pQueue = new LinkedList<>();
13         Queue<int[]> aQueue = new LinkedList<>();
14         for(int i=0; i<n; i++){ //Vertical border
15             pQueue.offer(new int[]{i, 0});
16             aQueue.offer(new int[]{i, m-1});
17             pacific[i][0] = true;
18             atlantic[i][m-1] = true;
19         }
20         for(int i=0; i<m; i++){ //Horizontal border
21             pQueue.offer(new int[]{0, i});
22             aQueue.offer(new int[]{n-1, i});
23             pacific[0][i] = true;
24             atlantic[n-1][i] = true;
25         }
26         bfs(matrix, pQueue, pacific);
27         bfs(matrix, aQueue, atlantic);
28         for(int i=0; i<n; i++){
29             for(int j=0; j<m; j++){
30                 if(pacific[i][j] && atlantic[i][j])
31                     res.add(new int[]{i,j});
32             }
33         }
34         return res;
35     }
36     public void bfs(int[][]matrix, Queue<int[]> queue, boolean[][]visited){
37         int n = matrix.length, m = matrix[0].length;
38         while(!queue.isEmpty()){
39             int[] cur = queue.poll();
40             for(int[] d:dir){
41                 int x = cur[0]+d[0];
42                 int y = cur[1]+d[1];
43                 if(x<0 || x>=n || y<0 || y>=m || visited[x][y] || matrix[x][y] > matrix[cur[0]][cur[1]]){
44                     continue;
45                 }
46                 visited[x][y] = true;
47                 queue.offer(new int[]{x, y});
48             }
49         }
50     }
51 }
时间: 2024-10-29 19:06:03

Leetcode: Pacific Atlantic Water Flow的相关文章

[LeetCode] Pacific Atlantic Water Flow 太平洋大西洋水流

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. Wate

Pacific Atlantic Water Flow 解答

Question 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 ed

417. Pacific Atlantic Water Flow

这题考点在于需要设置两个visited数组 dfs 与bfs不同之处在于使调用符合题意的自己, 而bfs是遍历符合条件的兄弟, 用Queue public class Solution { int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; public List<int[]> pacificAtlantic(int[][] matrix) { List<int[]> res = new Array

[leetcode]Trapping Rain Water @ Python

原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,

[LeetCode] [Trapping Rain Water 2012-03-10]

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented by a

LeetCode: Trapping Rain Water [041]

[题目] Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented

LeetCode OJ-- Trapping Rain Water*

https://oj.leetcode.com/problems/trapping-rain-water/ 模拟题,计算出在凹凸处存水量. 对于一个位置 i ,分别计算出它左边的最大值 left (从左扫描一遍), 右边的最大值 right(从右扫描一遍) .找left right中的最小值,如果大于 A[i],则做 min - A[i] 的累加. class Solution { public: int trap(int A[], int n) { if(n<=1) return 0; vec

LeetCode——Trapping Rain Water

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented by a

LeetCode: Trapping Rain Water 解题报告

https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,