当前位置:网站首页>[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))
边栏推荐
- 高考录取分数线爬取
- 08_ strand
- Evaluation of embedded rz/g2l processor core board and development board of Feiling
- 【LeetCode】977-有序數組的平方
- Leetcode skimming -- sum of two integers 371 medium
- How to choose a third-party software testing organization for automated acceptance testing of mobile applications
- 10_Redis_geospatial_命令
- 【LeetCode】977-有序数组的平方
- Set set you don't know
- 让您的HMI更具优势,FET-G2LD-C核心板是个好选择
猜你喜欢
随机推荐
08_ strand
Semantic segmentation learning notes (1)
[network security] network asset collection
语义分割学习笔记(一)
[leetcode] 167 - sum of two numbers II - enter an ordered array
做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
16_ Redis_ Redis persistence
[solution] educational codeforces round 82
List set & UML diagram
Force deduction solution summary 2029 stone game IX
03. Preliminary use of golang
夏季高考文化成绩一分一段表
[development environment] install the Chinese language pack for the 2013 version of visual studio community (install test agents 2013 | install visual studio 2013 simplified Chinese)
12_ Redis_ Bitmap_ command
[leetcode] 200 number of islands
让您的HMI更具优势,FET-G2LD-C核心板是个好选择
NBA player analysis
How to write sensor data into computer database
10_ Redis_ geospatial_ command
【LeetCode】189-轮转数组