LeetCode 417. Pacific Atlantic Water Flow

原题链接在这里:https://leetcode.com/problems/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:

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

题解:

能流淌到左边界 和 上边界的就是能流到Pacific. 那么反过来左边界 和 上边界的点反过来, 往高走 能到达的点就都是能流到Pacific的点.

从左边界和上边界所有点BFS 能visit到的点都是能流到Pacific的点.

同理, 从右边界和下边界所有点BFS能visit道德点都是能留到Atlantic的点. 若有交集 就是 能流到both Pacific and Atlantic的点.

扫描二维码关注公众号,回复: 3107049 查看本文章

Time Complexity: O(m*n). m = matrix.length. n = matrix[0].length.

Space: O(m*n).

AC Java:

 1 class Solution {
 2     public List<int[]> pacificAtlantic(int[][] matrix) {
 3         List<int []> res = new ArrayList<int []>();
 4         if(matrix == null || matrix.length == 0|| matrix[0].length == 0){
 5             return res;
 6         }
 7         
 8         int m = matrix.length;
 9         int n = matrix[0].length;
10         int [][] dirs = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
11         
12         // 左上BFS
13         boolean [][] visitedUpLeft = new boolean[m][n];
14         LinkedList<int []> que = new LinkedList<int []>();
15         for(int i = 0; i<m; i++){
16             if(!visitedUpLeft[i][0]){
17                 visitedUpLeft[i][0] = true;
18                 que.add(new int[]{i, 0});
19             }
20         }
21         
22         for(int j = 0; j<n; j++){
23             if(!visitedUpLeft[0][j]){
24                 visitedUpLeft[0][j] = true;
25                 que.add(new int[]{0, j});
26             }
27         }
28         
29         while(!que.isEmpty()){
30             int [] cur = que.poll();
31             for(int [] dir : dirs){
32                 int x = cur[0] + dir[0];
33                 int y = cur[1] + dir[1];
34                 if(x<0 || x>=m || y<0 || y>=n || visitedUpLeft[x][y] || matrix[x][y]<matrix[cur[0]][cur[1]]){
35                     continue;
36                 }
37                 
38                 visitedUpLeft[x][y] = true;
39                 que.add(new int[]{x, y});
40             }
41         }
42         
43         // 右下BFS
44         boolean [][] visitedLowRight = new boolean[m][n];
45         for(int i = 0; i<m; i++){
46             if(!visitedLowRight[i][n-1]){
47                 visitedLowRight[i][n-1] = true;
48                 que.add(new int[]{i, n-1});
49             }
50         }
51         
52         for(int j = 0; j<n; j++){
53             if(!visitedLowRight[m-1][j]){
54                 visitedLowRight[m-1][j] = true;
55                 que.add(new int[]{m-1, j});
56             }
57         }
58         
59         while(!que.isEmpty()){
60             int [] cur = que.poll();
61             if(visitedUpLeft[cur[0]][cur[1]]){
62                 res.add(cur);
63             }
64             
65             for(int [] dir : dirs){
66                 int x = cur[0] + dir[0];
67                 int y = cur[1] + dir[1];
68                 if(x<0 || x>=m || y<0 || y>=n || visitedLowRight[x][y] || matrix[x][y]<matrix[cur[0]][cur[1]]){
69                     continue;
70                 }
71                 
72                 visitedLowRight[x][y] = true;
73                 que.add(new int[]{x, y});
74             }
75         }
76         
77         return res;
78     }
79 }

猜你喜欢

转载自www.cnblogs.com/Dylan-Java-NYC/p/9616625.html