博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
同时被两种海水经过的点的坐标 Pacific Atlantic Water Flow
阅读量:7064 次
发布时间:2019-06-28

本文共 3687 字,大约阅读时间需要 12 分钟。

hot3.png

问题:

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  *          *   *   *   *   * AtlanticReturn:[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).

解决:

① 水可以向上下左右四个方向流,但是只能向高度小于等于当前高度的方向流,返回同时被两种海水经过的点的坐标。

1、分别处理每个海洋,从海洋边缘(每个海洋两条边)开始,一步步的搜索,即从连接海洋的地方开始搜索,哪些节点的高度大于等于自身(反过来就是可以从那里流到自己),如果是,就标记为true,就这样不断搜索下去。最后所有标记为true的位置就是可以流到对应的海洋。 

2、找这两个矩阵,同为true的输出,就是都能流到两个海洋的位置

dfs解决:

class Solution { //22ms

    public List<int[]> pacificAtlantic(int[][] matrix) {
        List<int[]> 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[][] atlantic = new boolean[m][n];
        //递归遍历每个位置
        for (int i = 0;i < m;i ++){//由第一列和最后一列开始递归遍历
            dfs(matrix,i,0,pacific,Integer.MIN_VALUE);
            dfs(matrix,i,n - 1,atlantic,Integer.MIN_VALUE);
        }
        for (int j = 0;j < n;j ++){//由第一行和最后一行开始递归遍历
            dfs(matrix,0,j,pacific,Integer.MIN_VALUE);
            dfs(matrix,m - 1,j,atlantic,Integer.MIN_VALUE);
        }
        //查找两个海洋的交集
        for (int i = 0;i < m;i ++){
            for (int j = 0;j < n;j ++){
                if (pacific[i][j] && atlantic[i][j]){
                    res.add(new int[]{i,j});
                }
            }
        }
        return res;
    }
    public void dfs(int[][] matrix,int i,int j,boolean[][] isvisited,int pre){
        int m = matrix.length;
        int n = matrix[0].length;
        if (i < 0 || i >= m || j < 0 || j >= n || isvisited[i][j] || matrix[i][j] < pre){
            return;
        }
        isvisited[i][j] = true;
        dfs(matrix,i + 1,j,isvisited,matrix[i][j]);
        dfs(matrix,i - 1,j,isvisited,matrix[i][j]);
        dfs(matrix,i,j + 1,isvisited,matrix[i][j]);
        dfs(matrix,i,j - 1,isvisited,matrix[i][j]);
    }
}

② bfs解决:

class Solution { //31ms

    public List<int[]> pacificAtlantic(int[][] matrix) {
        List<int[]> 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[][] atlantic = new boolean[m][n];
        Queue<int[]> q1 = new LinkedList<>();
        Queue<int[]> q2 = new LinkedList<>();
        for (int i = 0;i < m;i ++){
            q1.offer(new int[]{i,0});
            q2.offer(new int[]{i,n - 1});
            pacific[i][0] = true;
            atlantic[i][n - 1] = true;
        }
        for (int j = 0;j < n;j ++){
            q1.offer(new int[]{0,j});
            q2.offer(new int[]{m - 1,j});
            pacific[0][j] = true;
            atlantic[m - 1][j] = true;
        }
        bfs(matrix,pacific,q1);
        bfs(matrix,atlantic,q2);
        for (int i = 0;i < m;i ++){
            for (int j = 0;j < n;j ++){
                if (pacific[i][j] && atlantic[i][j]){
                    res.add(new int[]{i,j});
                }
            }
        }
        return res;
    }
    public void bfs(int[][] matrix,boolean[][] isvisited,Queue<int[]> q){
        int m = matrix.length;
        int n = matrix[0].length;
        int[][] dirs = {
{0,-1},{-1,0},{0,1},{1,0}};
        while(! q.isEmpty()){
            int[] tmp = q.poll();
            for (int[] dir : dirs){
                int x = tmp[0] + dir[0];
                int y = tmp[1] + dir[1];
                if (x < 0 || x >= m || y < 0 || y >= n || isvisited[x][y] || matrix[x][y] < matrix[tmp[0]][tmp[1]]){
                    continue;
                }
                isvisited[x][y] = true;
                q.offer(new int[]{x,y});
            }
        }
    }
}

转载于:https://my.oschina.net/liyurong/blog/1601476

你可能感兴趣的文章
IDS与snort
查看>>
Understanding memory usage on Linux
查看>>
Oracle浅谈之逻辑体系第一回
查看>>
RequireJS 基础学习
查看>>
MyEclipse10安装PyDev插件
查看>>
C++ 0x 之左值与右值、右值引用、移动语义、传导模板
查看>>
KeyShot中的环境光遮蔽该怎么进行使用
查看>>
GIT 不常用的实用命令
查看>>
一个简单的替换Emoji表情字符的方法
查看>>
Server should be SSL-aware but has no certificate
查看>>
ubuntu16.04安装mysql并配置远程访问
查看>>
poj 选择客栈
查看>>
加上线程池后Netty的Reacotr模型,两张图说明问题
查看>>
notepad++中emmet插件的使用
查看>>
Notepad++对.tpl默认打开且高亮显示HTML代码
查看>>
How The Kernel Manages Your Memory
查看>>
LINUX的学习路上的锁锁碎碎
查看>>
spring MVC 3.1 forword/redirect
查看>>
移动端压缩上传图片
查看>>
利用哈希表实现数组的去重以及利用delete批量删除数组元素
查看>>