当前位置:网站首页>One question per day 1765 The highest point in the map
One question per day 1765 The highest point in the map
2022-07-05 05:42:00 【A big pigeon】
topic : Find the height of the highest point that may exist on a two-dimensional map . That is, give the water area on the map , Find a way to fill the blank area with land .
Input m*n Two dimensional array of isWater, Indicates whether the location is a water area .1 yes 0 no .
When calculating height , The height of the water area is 0. And the height difference between adjacent lands is at most 1.
Explain :
Refer to the explanation of the question . breadth-first BFS, Start from the water , Fill adjacent and unset grids .
class Solution:
def highestPeak(self, isWater: List[List[int]]) -> List[List[int]]:
m, n = len(isWater), len(isWater[0])
ans = [[water-1 for water in row] for row in isWater] # initialization , Set the water height to 0. Land initialization -1
q = deque((i,j) for i, row in enumerate(isWater) for j,water in enumerate(row) if water)# Join the team
while q:
i, j = q.popleft()
for x,y in ((i-1,j),(i+1, j), (i, j-1), (i, j+1)):
if 0<=x<m and 0<=y<n and ans[x][y] == -1:
ans[x][y] = ans[i][j] +1
q.append((x,y))
return ans
Add :
1.Python The comparison operation in can be concatenated arbitrarily ; for example ,x < y <= z
Equivalent to x < y and y <= z.
2.deque The bidirectional queue , Adding or popping elements can be bidirectional .
collections --- Container data type — Python 3.10.2 file
append(), pop() From the right end .
appendleft(), popleft() It's from the left .
边栏推荐
- Talking about JVM (frequent interview)
- AtCoder Grand Contest 013 E - Placing Squares
- 游戏商城毕业设计
- [cloud native] record of feign custom configuration of microservices
- Some common problems in the assessment of network engineers: WLAN, BGP, switch
- 26、 File system API (device sharing between applications; directory and file API)
- Zzulioj 1673: b: clever characters???
- High precision subtraction
- Maximum number of "balloons"
- Sword finger offer 58 - ii Rotate string left
猜你喜欢
Time of process
剑指 Offer 53 - II. 0~n-1中缺失的数字
Solution to game 10 of the personal field
剑指 Offer 06.从头到尾打印链表
CF1634 F. Fibonacci Additions
Scope of inline symbol
Sword finger offer 09 Implementing queues with two stacks
【Jailhouse 文章】Look Mum, no VM Exits
Sword finger offer 05 Replace spaces
lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
随机推荐
Haut OJ 2021 freshmen week II reflection summary
每日一题-无重复字符的最长子串
Sword finger offer 53 - I. find the number I in the sorted array
PC寄存器
How can the Solon framework easily obtain the response time of each request?
Palindrome (csp-s-2021-palin) solution
Full Permutation Code (recursive writing)
Developing desktop applications with electron
智慧工地“水电能耗在线监测系统”
Sword finger offer 53 - ii Missing numbers from 0 to n-1
Add level control and logger level control of Solon logging plug-in
Fried chicken nuggets and fifa22
kubeadm系列-01-preflight究竟有多少check
Maximum number of "balloons"
Configuration and startup of kubedm series-02-kubelet
R language [import and export of dataset]
Smart construction site "hydropower energy consumption online monitoring system"
剑指 Offer 05. 替换空格
卷积神经网络简介
Csp-j-2020-excellent split multiple solutions