Leetcode 417 Pacific Atlantic Water Flow

Leetcode 417 Pacific Atlantic Water Flow

Given the following 5x5 matrix:
Pacific at top and left;
Atlantic at bottom and right;

  • 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

Return:
[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).

Explanation of solution array (using shortest paths):

  • [0, 4], value 5 – Path to Pacific (Up) – Path to Atlantic (Right)
  • [1, 3], value 4 – Path to Pacific (Up, Up) – Path to Atlantic (Right, Right)
  • [1, 4], value 4 – Path to Pacific (Left, Up, Up) --Path to Atlantic (Right)
  • [2, 2], value 5 – Path to Pacific (Up, Up, Up) or (Left, Left, Left) – Path to Atlantic (Right, Right, Right) or (Down, Down, Down)
  • [3, 0], value 6 – Path to Pacific (Left) --Path to Atlantic (Down, Down)
  • [3, 1], value 7 – Path to Pacific (Left, Left) – Path to Atlantic (Down, Down)
  • [4, 0], value 5 – Path to Pacific (Left) – Path to Atlantic (Down)

Points of discussion:
If point [1, 3] with value 4 was changed to a value of 3, then it would act like passage through the “ridge line” allowing many more points to reach the Pacific, and the solution array would be: [[0, 3], [0, 4], [1, 2], [1, 3], [1, 4], [2, 2], [2, 3], [3, 0], [3, 1], [3, 3], [3, 4], [4, 0]]

  • 1 2 2 (3) (5)
  • 3 2 (3) (3) (4)
  • 2 4 (5) (3) 1
  • (6) (7) 1 (4) (5)
  • (5) 1 1 2 4

BFS AND DFS

Algorithm:

  1. Two Queue and add all the Pacific border to one queue; Atlantic border to another queue.先使用queue存储Pacific边界和Atlantic边界。
  2. Keep a visited matrix for each queue. In the end, add the cell visited by two queue to the result. 每一个Queue都有visited 2-d array,使用visited二维数组记录访问过哪些点。
  3. BFS: Wate 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. 使用BFS遍历两个queue的点,判断是否同时满足Pacific visited and Atlantic visited。
public class Solution {
    int[][]dir = new int[][]{{1,0},{-1,0},{0,1},{0,-1}};
    public List<int[]> pacificAtlantic(int[][] matrix) {
        List<int[]> res = new LinkedList<>();
        if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
            return res;
        }
        int n = matrix.length, m = matrix[0].length;
        //One visited map for each ocean
        boolean[][] pacific = new boolean[n][m];
        boolean[][] atlantic = new boolean[n][m];
        Queue<int[]> pQueue = new LinkedList<>();
        Queue<int[]> aQueue = new LinkedList<>();
        for(int i=0; i<n; i++){ //Vertical border
            pQueue.offer(new int[]{i, 0});
            aQueue.offer(new int[]{i, m-1});
            pacific[i][0] = true;
            atlantic[i][m-1] = true;
        }
        for(int i=0; i<m; i++){ //Horizontal border
            pQueue.offer(new int[]{0, i});
            aQueue.offer(new int[]{n-1, i});
            pacific[0][i] = true;
            atlantic[n-1][i] = true;
        }
        bfs(matrix, pQueue, pacific);
        bfs(matrix, aQueue, atlantic);
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                if(pacific[i][j] && atlantic[i][j])
                    res.add(new int[]{i,j});
            }
        }
        return res;
    }
    public void bfs(int[][]matrix, Queue<int[]> queue, boolean[][]visited){
        int n = matrix.length, m = matrix[0].length;
        while(!queue.isEmpty()){
            int[] cur = queue.poll();
            for(int[] d:dir){
                int x = cur[0]+d[0];
                int y = cur[1]+d[1];
                if(x<0 || x>=n || y<0 || y>=m || visited[x][y] || matrix[x][y] < matrix[cur[0]][cur[1]]){
                    continue;
                }
                visited[x][y] = true;
                queue.offer(new int[]{x, y});
            } 
        }
    }
}

bfs and bfs seem has same time complexity.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值