Leetcode: 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).
这题考点在于需要设置两个visited数组
Two Queue and add all the Pacific border to one queue; Atlantic border to another queue.
Keep a visited matrix for each queue. In the end, add the cell visited by two queue to the result.
BFS: Water 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.(逆流而上)
Solution 1: 我自己的DFS (beat 89%)
public class Solution {
int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
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 n = matrix.length, m = matrix[0].length;
boolean[][] pVisited = new boolean[n][m];
boolean[][] aVisited = new boolean[n][m];
for (int i=0; i<n; i++) {
//pacific
dfs(matrix, i, 0, pVisited);
//atlatic
dfs(matrix, i, m-1, aVisited);
} for (int j=0; j<m; j++) {
//pacific
dfs(matrix, 0, j, pVisited);
//atlatic
dfs(matrix, n-1, j, aVisited);
} for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
if (pVisited[i][j] && aVisited[i][j])
res.add(new int[]{i, j});
}
}
return res;
} public void dfs(int[][] matrix, int i, int j, boolean[][] visited) {
int n = matrix.length, m = matrix[0].length;
visited[i][j] = true;
for (int[] dir : directions) {
int row = dir[0] + i;
int col = dir[1] + j;
if (row>=0 && row<n && col>=0 && col<m && !visited[row][col] && matrix[i][j]<=matrix[row][col])
dfs(matrix, row, col, visited);
}
}
}
Solution 2: BFS, refer to https://discuss.leetcode.com/topic/62379/java-bfs-dfs-from-ocean/2
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});
}
}
}
}
Leetcode: Pacific Atlantic Water Flow的更多相关文章
- [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,其访问顺序应该是找到下一个比当前那个要 ...
- LeetCode 417. Pacific Atlantic Water Flow
原题链接在这里:https://leetcode.com/problems/pacific-atlantic-water-flow/description/ 题目: Given an m x n ma ...
- [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- ...
- [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 ...
- 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] Trapping Rain Water II 收集雨水之二
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
随机推荐
- NOI模拟赛Day2
深深的感受到了自己的水 ---------------------------------------------------------------------------------------- ...
- Spring MVC中处理静态资源的多种方法
处理静态资源,我想这可能是框架搭建完成之后Web开发的”头等大事“了. 因为一个网站的显示肯定会依赖各种资源:脚本.图片等,那么问题来了,如何在页面中请求这些静态资源呢? 还记得Spring MVC中 ...
- docker WARNING: bridge-nf-call-iptables is disabled 处理
在CentOS中 vim /etc/sysctl.conf net.bridge.bridge-nf-call-ip6tables = net.bridge.bridge-nf-call-iptabl ...
- Express 路由
路由 路由是指如何定义应用的端点(URIs)以及如何响应客户端的请求. 路由是由一个 URI.HTTP 请求(GET.POST等)和若干个句柄组成,它的结构如下: app.METHOD(path, [ ...
- JS文档生成工具:JSDoc 介绍
JSDoc是一个根据javascript文件中注释的信息,生成API文档的工具.生成的文档是html文件.类似JavaDoc和PHPDoc. 用法 /** 一坨注释之类的 */JSDoc会从/**开头 ...
- window.open()&&window.showmodaldialog()
open 打开一个新窗口,并装载URL指定的文档,或装载一个空白文档,如果没提供URL的话. 适用于 窗口 语法 window = object.open([URL[,name[,features[, ...
- boostrap折叠,jquery ui accordion同时打开多个标签
http://caibaojian.com/bootstrap/javascript.html http://www.w3cschool.cc/jqueryui/example-accordion.h ...
- WordPress插件入口菜单创建的位置代码
Add_management_page() 在Tools下面创建 Add_options_page() 在Settings下面创建 Add_theme_page() 在Appearance下面创建 A ...
- html 锚点的使用
html 锚点 到底是干吗的?通俗简单地说,比如一篇很长的文章,你想按分段精确来看,那就可以用到锚点了. 代码:<a href="#001">跳到001</a&g ...
- YII2 blockui
https://packagist.org/packages/ayrozjlc/yii2-blockui