当前位置:网站首页>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 .
边栏推荐
- 剑指 Offer 09. 用两个栈实现队列
- shared_ Repeated release heap object of PTR hidden danger
- Convolution neural network -- convolution layer
- kubeadm系列-02-kubelet的配置和启动
- Alu logic operation unit
- Brief introduction to tcp/ip protocol stack
- 个人开发的渗透测试工具Satania v1.2更新
- Pointnet++ learning
- SSH password free login settings and use scripts to SSH login and execute instructions
- Implement a fixed capacity stack
猜你喜欢

YOLOv5-Shufflenetv2

剑指 Offer 53 - I. 在排序数组中查找数字 I

Solution to the palindrome string (Luogu p5041 haoi2009)

【实战技能】非技术背景经理的技术管理

Gbase database helps the development of digital finance in the Bay Area

Brief introduction to tcp/ip protocol stack

Web APIs DOM node

CF1634E Fair Share

剑指 Offer 58 - II. 左旋转字符串

Fried chicken nuggets and fifa22
随机推荐
How many checks does kubedm series-01-preflight have
ssh免密登录设置及使用脚本进行ssh登录并执行指令
Implement an iterative stack
High precision subtraction
剑指 Offer 53 - I. 在排序数组中查找数字 I
2022年贵州省职业院校技能大赛中职组网络安全赛项规程
Configuration and startup of kubedm series-02-kubelet
Reflection summary of Haut OJ freshmen on Wednesday
软件测试 -- 0 序
Sword finger offer 06 Print linked list from beginning to end
Hang wait lock vs spin lock (where both are used)
卷积神经网络——卷积层
Demonstration of using Solon auth authentication framework (simpler authentication framework)
Use of room database
记录QT内存泄漏的一种问题和解决方案
Using HashMap to realize simple cache
How can the Solon framework easily obtain the response time of each request?
Introduction et expérience de wazuh open source host Security Solution
Graduation project of game mall
挂起等待锁 vs 自旋锁(两者的使用场合)