博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Pacific Atlantic Water Flow
阅读量:6296 次
发布时间:2019-06-22

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

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.

BFS

Time Complexity

O(mn)
Space Complexity
O(mn)

思路

Add all boundary of Pacific ocean into queue. Do bfs and then reuse the same queue, if this point hasn't been visited and the value is bigger or equal to the current point value, then put it into queue and make it visited. Put all Atlantic ocean boundary into queue do the same bfs. We can't add one point to the queue twice, it will cause duplicate. If the point can be touched from Pacific ocean, add it into pac, if the point can be touched from Atlantic ocean, add it into alt. If one point is both in pac and alt, add it to res.

代码

public List
pacificAtlantic(int[][] matrix) { List
res = new ArrayList
(); if(matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) return res; int rows = matrix.length, cols = matrix[0].length; int[][] directions = new int[][]{
{-1, 0},{1, 0},{0, -1},{0,1}}; boolean[][] pac = new boolean[rows][cols]; boolean[][] alt = new boolean[rows][cols]; Queue
queue = new LinkedList
(); //add Pacific on left for(int i = 0; i < rows; i++){ queue.offer(new int[]{i,0}); pac[i][0] = true; } //add Pacific on top for(int j = 1; j < cols; j++){ queue.offer(new int[]{0,j}); pac[0][j] = true; } bfs(matrix, rows, cols, queue, pac, alt, directions, res); //add Atlantic on bottom for(int j = 0; j < cols - 1; j++){ queue.offer(new int[]{rows - 1, j}); alt[rows - 1][j] = true; } //add Atlantic on right for(int i = 0; i < rows; i++){ queue.offer(new int[]{i, cols - 1}); alt[i][cols - 1] = true; } bfs(matrix, rows, cols, queue, alt, pac, directions, res); return res;}private void bfs(int[][] matrix, int rows, int cols, Queue
queue, boolean[][] self, boolean[][] others, int[][] directions, List
res){ while(!queue.isEmpty()){ int[] cur = queue.poll(); if(others[cur[0]][cur[1]]) res.add(new int[]{cur[0],cur[1]}); for(int[] dir : directions){ int x = cur[0] + dir[0]; int y = cur[1] + dir[1]; if(x >= 0 && x < rows && y >= 0 && y < cols && self[x][y] == false && matrix[x][y] >= matrix[cur[0]][cur[1]]){ self[x][y] = true; queue.offer(new int[]{x,y}); } } }}

转载地址:http://mvvta.baihongyu.com/

你可能感兴趣的文章
什么是PyTorch,为何要使用PyTorch
查看>>
对ESB概念的理解(转)
查看>>
Building for Production
查看>>
python 内部函数,以及lambda,filter,map等内置函数
查看>>
大家猜猜看除了围棋,人工智能下一个颠覆的领域是什么?
查看>>
SharePoint 2013 数据库中手动更新用户信息
查看>>
SharePoint 2013 表单认证使用ASP.Net配置工具添加用户
查看>>
《C程序员:从校园到职场》出版预告(1):从“高大上”到“柴米油盐”
查看>>
李飞飞获全球最权威女性领导力奖 Athena Award,讲述推动AI多元化三大原因(视频)...
查看>>
线程堆栈大小 pthread_attr_setstacksize 的使用
查看>>
杀手洗车房:黑客能困住并攻击汽车
查看>>
云计算物联网Hold住未来十大技术趋势
查看>>
2016总结 - 我的转型之路
查看>>
优化Hadoop Balancer运行速度
查看>>
分析型数据库受大数据市场追捧
查看>>
深度学习训练,选择P100就对了
查看>>
ElasticSearch小操之Marvel,Sense
查看>>
[译] Redux 有多棒?
查看>>
Powershell 邮件发送
查看>>
创建代码生成器可以很简单:如何通过T4模板生成代码?[下篇]
查看>>