当前位置:   article > 正文

leetcode 417. Pacific Atlantic Water Flow

leetcode 417. 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:

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

使用DFS来做

  1. public class Solution {
  2. private int m=0, n=0;
  3. public List<int[]> pacificAtlantic(int[][] matrix) {
  4. List<int[]> ans = new ArrayList<>();
  5. if(matrix==null || matrix.length==0 || matrix[0].length==0) return ans;
  6. m=matrix.length;
  7. n=matrix[0].length;
  8. boolean[][] p = new boolean[m][n];
  9. boolean[][] a = new boolean[m][n];
  10. for(int i=0; i<m; i++){
  11. helper(matrix, p, i, 0, Integer.MIN_VALUE);
  12. helper(matrix, a, i, n-1, Integer.MIN_VALUE);
  13. }
  14. for(int i=0; i<n; i++){
  15. helper(matrix, p, 0, i, Integer.MIN_VALUE);
  16. helper(matrix, a, m-1, i, Integer.MIN_VALUE);
  17. }
  18. for(int i=0; i<m; i++){
  19. for(int j=0; j<n; j++){
  20. if(p[i][j] && a[i][j]) ans.add(new int[]{i, j});
  21. }
  22. }
  23. return ans;
  24. }
  25. int[][] dir = new int[][]{{0,1}, {1,0}, {0,-1}, {-1,0}};
  26. private void helper(int[][] matrix, boolean[][] visited, int i, int j, int pre){
  27. if(i<0 || i>=m || j<0 || j>=n || visited[i][j] || matrix[i][j]<pre) return;
  28. visited[i][j] = true;
  29. for(int[] d : dir) helper(matrix, visited, i+d[0], j+d[1], matrix[i][j]);
  30. }
  31. }

还有BFS的做法

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


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/146403
推荐阅读
相关标签
  

闽ICP备14008679号