当前位置:网站首页>[leetcode] 417 - Pacific Atlantic current problem
[leetcode] 417 - Pacific Atlantic current problem
2022-07-02 15:37:00 【Crisp ~】
There is one m × n Rectangular Island , And The Pacific Ocean and The Atlantic ocean adjacent . “ The Pacific Ocean ” On the left and upper boundary of the continent , and “ The Atlantic ocean ” On the right and lower boundaries of the continent .
The island is divided into a grid of square cells . Given a m x n The integer matrix of heights , heights[r][c] Representation coordinates (r, c) Upper cell Height above sea level .
There is more rain on the island , If the height of adjacent cells Less than or equal to The height of the current cell , The rain can go straight north 、 south 、 In the east 、 West flow to adjacent cells . Water can flow into the ocean from any cell near the ocean .
Return grid coordinates result Of 2D list , among result[i] = [ri, ci] Indicates that rainwater flows from the cell (ri, ci) flow It can flow to the Pacific Ocean or the Atlantic Ocean .
Example 1:

Input : heights = [[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]]
Output : [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
Example 2:
Input : heights = [[2,1],[1,2]]
Output : [[0,0],[0,1],[1,0],[1,1]]
Tips :
- m == heights.length
- n == heights[r].length
- 1 <= m, n <= 200
- 0 <= heights[r][c] <= 105
Ideas :
The topic requires that the output can flow to the Pacific Ocean at the same time 、 The grid coordinates of the Atlantic , Then you can turn it upside down , From the Pacific 、 The edge of the Atlantic Ocean pushes against , Get the grid coordinates that can flow to the Pacific Ocean and the grid coordinates that can flow to the Atlantic Ocean , When the two sets of data intersect, we can get the grid coordinates that can flow to the Pacific Ocean and the Atlantic Ocean at the same time
# Depth-first traversal
class Solution(object):
def pacificAtlantic(self, heights):
m,n = len(heights),len(heights[0])# Grid length and width
def search(arr):# Get all the grid coordinates that can flow to a ocean
visited = set()# Storage
def dfs(x,y):
if (x,y) in visited:
return
visited.add((x,y))
for xx,yy in [(x,y+1),(x,y-1),(x+1,y),(x-1,y)]:# Four directions
if 0<=xx<m and 0<=yy<n and heights[xx][yy]>=heights[x][y]:# Push against , So the height of the next piece should be greater than or equal to
dfs(xx,yy)
for x,y in arr:
dfs(x,y)
return visited
pacific = [(0,i) for i in range(n)]+[(i,0) for i in range(1,m)]
atlantic = [(m-1,i) for i in range(n)]+[(i,n-1) for i in range(m-1)]
return list(map(list,search(pacific)&search(atlantic)))
class Solution(object):
def pacificAtlantic(self, heights):
m,n = len(heights),len(heights[0])
pacific = [(0,i) for i in range(n)]+[(i,0) for i in range(1,m)]
atlantic = [(m-1,i) for i in range(n)]+[(i,n-1) for i in range(m-1)]
visited1 = set()
visited2 = set()
while pacific:
x,y = pacific[0]
pacific.pop(0)
if (x,y) not in visited1:
visited1.add((x,y))
for xx,yy in [(x,y+1),(x,y-1),(x+1,y),(x-1,y)]:
if 0<=xx<m and 0<=yy<n and heights[xx][yy]>=heights[x][y]:
pacific.append((xx,yy))
while atlantic:
x,y = atlantic[0]
atlantic.pop(0)
if (x,y) not in visited2:
visited2.add((x,y))
for xx,yy in [(x,y+1),(x,y-1),(x+1,y),(x-1,y)]:
if 0<=xx<m and 0<=yy<n and heights[xx][yy]>=heights[x][y]:
atlantic.append((xx,yy))
return list(map(list,visited1&visited2))
边栏推荐
- MySQL -- Index Optimization -- order by
- vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
- 【LeetCode】877-石子游戏
- 15_ Redis_ Redis. Conf detailed explanation
- 03_線性錶_鏈錶
- How to find a sense of career direction
- 08_ strand
- LeetCode刷题——奇偶链表#328#Medium
- yolo格式数据集处理(xml转txt)
- NBA player analysis
猜你喜欢

【LeetCode】417-太平洋大西洋水流问题

NBA player analysis

2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)

05_ queue

How does the computer set up speakers to play microphone sound

XML Configuration File

. Solution to the problem of Chinese garbled code when net core reads files

做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统

Wechat Alipay account system and payment interface business process

14_Redis_乐观锁
随机推荐
Loss function and positive and negative sample allocation: Yolo series
Pytorch 保存tensor到.mat文件
FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
工程师评测 | RK3568开发板上手测试
【LeetCode】876-链表的中间结点
MySQL -- Index Optimization -- order by
The traversal methods of binary tree mainly include: first order traversal, middle order traversal, second order traversal, and hierarchical traversal. First order, middle order, and second order actu
List set & UML diagram
Bing.com網站
Leetcode skimming -- count the number of numbers with different numbers 357 medium
搭建自己的语义分割平台deeplabV3+
Bing. Site Internet
【LeetCode】417-太平洋大西洋水流问题
微信支付宝账户体系和支付接口业务流程
How does the computer set up speakers to play microphone sound
Map introduction
[leetcode] 344 reverse string
03.golang初步使用
[solution] educational codeforces round 82
Set set you don't know