当前位置:网站首页>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 .
边栏推荐
- Web APIs DOM node
- 第六章 数据流建模—课后习题
- Brief introduction to tcp/ip protocol stack
- Codeforces round 712 (Div. 2) d. 3-coloring (construction)
- [jailhouse article] look mum, no VM exits
- YOLOv5-Shufflenetv2
- Sword finger offer 04 Search in two-dimensional array
- Annotation and reflection
- 利用HashMap实现简单缓存
- 剑指 Offer 06.从头到尾打印链表
猜你喜欢
[cloud native] record of feign custom configuration of microservices
Sword finger offer 05 Replace spaces
CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five
第六章 数据流建模—课后习题
Light a light with stm32
全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析
Scope of inline symbol
Introduction to tools in TF-A
用STM32点个灯
剑指 Offer 04. 二维数组中的查找
随机推荐
网络工程师考核的一些常见的问题:WLAN、BGP、交换机
常见的最优化方法
用STM32点个灯
Gbase database helps the development of digital finance in the Bay Area
Kubedm series-00-overview
Hang wait lock vs spin lock (where both are used)
Codeforces round 712 (Div. 2) d. 3-coloring (construction)
A new micro ORM open source framework
Reader writer model
How can the Solon framework easily obtain the response time of each request?
Sword finger offer 05 Replace spaces
注解与反射
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
Web APIs DOM node
Brief introduction to tcp/ip protocol stack
How many checks does kubedm series-01-preflight have
个人开发的渗透测试工具Satania v1.2更新
Talking about JVM (frequent interview)
Control Unit 控制部件
全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析