当前位置:网站首页>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 .
边栏推荐
- YOLOv5-Shufflenetv2
- kubeadm系列-00-overview
- Chapter 6 data flow modeling - after class exercises
- Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
- Codeforces round 712 (Div. 2) d. 3-coloring (construction)
- Csp-j-2020-excellent split multiple solutions
- Brief introduction to tcp/ip protocol stack
- Summary of Haut OJ 2021 freshman week
- 数仓项目的集群脚本
- [practical skills] how to do a good job in technical training?
猜你喜欢
【云原生】微服务之Feign自定义配置的记录
剑指 Offer 58 - II. 左旋转字符串
Dichotomy, discretization, etc
Sword finger offer 05 Replace spaces
游戏商城毕业设计
【Jailhouse 文章】Jailhouse Hypervisor
In this indifferent world, light crying
Typical use cases for knapsacks, queues, and stacks
CF1637E Best Pair
Sword finger offer 04 Search in two-dimensional array
随机推荐
Time complexity and space complexity
Over fitting and regularization
每日一题-搜索二维矩阵ps二维数组的查找
Summary of Haut OJ 2021 freshman week
Brief introduction to tcp/ip protocol stack
ALU逻辑运算单元
Haut OJ 2021 freshmen week II reflection summary
SSH password free login settings and use scripts to SSH login and execute instructions
Corridor and bridge distribution (csp-s-2021-t1) popular problem solution
CF1634 F. Fibonacci Additions
The number of enclaves
用STM32点个灯
Educational Codeforces Round 107 (Rated for Div. 2) E. Colorings and Dominoes
shared_ Repeated release heap object of PTR hidden danger
全排列的代码 (递归写法)
搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
2017 USP Try-outs C. Coprimes
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
Gbase database helps the development of digital finance in the Bay Area
Common optimization methods