LeetCode 417. Pacific Atlantic Water Flow
原题链接在这里:https://leetcode.com/problems/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).
题解:
能流淌到左边界 和 上边界的就是能流到Pacific. 那么反过来左边界 和 上边界的点反过来, 往高走 能到达的点就都是能流到Pacific的点.
从左边界和上边界所有点BFS 能visit到的点都是能流到Pacific的点.
同理, 从右边界和下边界所有点BFS能visit道德点都是能留到Atlantic的点. 若有交集 就是 能流到both Pacific and Atlantic的点.
Time Complexity: O(m*n). m = matrix.length. n = matrix[0].length.
Space: O(m*n).
AC Java:
class Solution {
public List<int[]> pacificAtlantic(int[][] matrix) {
List<int []> res = new ArrayList<int []>();
if(matrix == null || matrix.length == 0|| matrix[0].length == 0){
return res;
}
int m = matrix.length;
int n = matrix[0].length;
int [][] dirs = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
// 左上BFS
boolean [][] visitedUpLeft = new boolean[m][n];
LinkedList<int []> que = new LinkedList<int []>();
for(int i = 0; i<m; i++){
if(!visitedUpLeft[i][0]){
visitedUpLeft[i][0] = true;
que.add(new int[]{i, 0});
}
}
for(int j = 0; j<n; j++){
if(!visitedUpLeft[0][j]){
visitedUpLeft[0][j] = true;
que.add(new int[]{0, j});
}
}
while(!que.isEmpty()){
int [] cur = que.poll();
for(int [] dir : dirs){
int x = cur[0] + dir[0];
int y = cur[1] + dir[1];
if(x<0 || x>=m || y<0 || y>=n || visitedUpLeft[x][y] || matrix[x][y]<matrix[cur[0]][cur[1]]){
continue;
}
visitedUpLeft[x][y] = true;
que.add(new int[]{x, y});
}
}
// 右下BFS
boolean [][] visitedLowRight = new boolean[m][n];
for(int i = 0; i<m; i++){
if(!visitedLowRight[i][n-1]){
visitedLowRight[i][n-1] = true;
que.add(new int[]{i, n-1});
}
}
for(int j = 0; j<n; j++){
if(!visitedLowRight[m-1][j]){
visitedLowRight[m-1][j] = true;
que.add(new int[]{m-1, j});
}
}
while(!que.isEmpty()){
int [] cur = que.poll();
if(visitedUpLeft[cur[0]][cur[1]]){
res.add(cur);
}
for(int [] dir : dirs){
int x = cur[0] + dir[0];
int y = cur[1] + dir[1];
if(x<0 || x>=m || y<0 || y>=n || visitedLowRight[x][y] || matrix[x][y]<matrix[cur[0]][cur[1]]){
continue;
}
visitedLowRight[x][y] = true;
que.add(new int[]{x, y});
}
}
return res;
}
}
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 contin ...
- 【LeetCode】417. Pacific Atlantic Water Flow 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/pacific- ...
- 417 Pacific Atlantic Water Flow 太平洋大西洋水流
详见:https://leetcode.com/problems/pacific-atlantic-water-flow/description/ C++: class Solution { publ ...
- 417. Pacific Atlantic Water Flow
正常做的,用了645MS..感觉DFS的时候剪枝有问题.. 为了剪枝可能需要标记一个点的4种情况: 1:滨临大西洋,所有太平洋来的点可以通过: 2:濒临太平洋,所有大西洋来的点可以通过: 3:都不濒临 ...
- [LeetCode] Pacific Atlantic Water Flow 太平洋大西洋水流
Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...
- Leetcode: Pacific Atlantic Water Flow
Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...
- [LeetCode] Pacific Atlantic Water Flow 题解
题意 题目 思路 一开始想用双向广搜来做,找他们相碰的点,但是发现对其的理解还是不够完全,导致没写成功.不过,后来想清楚了,之前的错误可能在于从边界点进行BFS,其访问顺序应该是找到下一个比当前那个要 ...
- [Swift]LeetCode417. 太平洋大西洋水流问题 | Pacific Atlantic Water Flow
Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
随机推荐
- angularjs1 自定义图片查看器(可旋转、放大、缩小、拖拽)
笔记: angularjs1 制作自定义图片查看器(可旋转.放大.缩小.拖拽) 2018-01-12 更新 可以在我的博客 查看我 已经封装好的 纯 js写的图片查看器插件 博客链接 懒得把 ...
- Java接受键盘输入
import java.util.Scanner;//方法1 import java.io.BufferedReader;//方法2 import java.io.IOException;//方法3 ...
- Git常用命令和Git团队使用规范指南
转自:https://wsgzao.github.io/post/git/ 前言 在2005年的某一天,Linux之父Linus Torvalds 发布了他的又一个里程碑作品——Git.它的出现改变了 ...
- iometer测试磁盘IO性能
of Outstanding I/Os per target – 被选中worker的每个磁盘一次所允许的未处理的异步I/O的数量.模拟测试多个应用向 IO 请求读写,默认是 1.通常不用这个参数,除 ...
- [java]BoneCP 参数详解
BoneCP 参数详解: ======================================== 一.BoneCP配置文件格式(bonecp-config.xml): xml versio ...
- MongoDB 3.4 分片集群副本集 认证
连接到router所在的MongoDB Shell 我本机端口设置在50000上 mongo --port 接下来的流程和普通数据库添加用户权限一样 db.createUser({user:&quo ...
- 【Error】:10061由于目标计算机积极拒绝,无法连接
之前Windows上连接mongodb的时候首先用mongod.exe启动程序之后,用mongo.exe来连接数据库.但是在连接的时候,出现如下错误: error:10061 由于目标计算机积极拒绝, ...
- 安装使用babel-polyfill。让IE支持es6
安装 npm install --save-dev babel-polyfill 使用 在你的代码头部加载babel-polyfill,注意一定要在你的代码开始前,第一个js文件的顶部.如果是vue在 ...
- CentOS 6.5 升级内核到 3.10.28
本文适用于CentOS 6.4, CentOS 6.5,亲测可行,估计也适用于其他Linux发行版. 1. 准备工作 1.1 下载源码包 Linux内核版本有两种:稳定版和开发版 ,Linux内核版本 ...
- 使用 Express 实现一个简单的 SPA 静态资源服务器
背景 限制 SPA 应用已经成为主流,在项目开发阶段产品经理和后端开发同学经常要查看前端页面,下面就是我们团队常用的使用 express 搭建的 SPA 静态资源服务器方案. 为 SPA 应用添加入口 ...