当前位置:网站首页>接雨水问题解析
接雨水问题解析
2022-07-06 11:32:00 【小小草帽】
题目描述
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
示例 1:
输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
输出:6
解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。
示例 2:
输入:height = [4,2,0,3,2,5]
输出:9
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/trapping-rain-water
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
方法:双指针
每个位置的最大接水量 = 左右指针位置对应高度小的一端的端点对应的当前最大高度 - 当前位置的高度
利用left和right指针分别从数组两端扫描。
判断接水量和指针移动条件。
总是优先移动左右指针位置高度小的指针。
代码
class Solution(object):
def trap(self, height):
""" :type height: List[int] :rtype: int """
ans = 0
left, right = 0, len(height) - 1
leftMax = rightMax = 0
while left < right: //从左右两端开始计算
leftMax = max(leftMax, height[left]) //更新左端点最大高度
rightMax = max(rightMax, height[right]) //更新右端点最大高度
if height[left] < height[right]: //判断左端点和右端点的大小,将高度小的一端向中间移动
ans += leftMax - height[left] //计算左端点可以接的最大雨水
left += 1
else:
ans += rightMax - height[right] //计算右端点可以接的最大雨水
right -= 1
return ans
边栏推荐
- Precautions for binding shortcut keys of QPushButton
- In 50W, what have I done right?
- The list of people who passed the fifth phase of personal ability certification assessment was published
- spark基础-scala
- 五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
- 全套教学资料,阿里快手拼多多等7家大厂Android面试真题
- R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置add参数为不同水平点状条带图添加箱图
- Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
- A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
- Dark horse -- redis
猜你喜欢

Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up
![打家劫舍III[后序遍历与回溯+动态规划]](/img/9b/e9eeed138e46afdeed340bf2629ee1.png)
打家劫舍III[后序遍历与回溯+动态规划]

思维导图+源代码+笔记+项目,字节跳动+京东+360+网易面试题整理

倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告

多线程基础:线程基本概念与线程的创建

渲大师携手向日葵,远控赋能云渲染及GPU算力服务
![Looting iii[post sequence traversal and backtracking + dynamic planning]](/img/9b/e9eeed138e46afdeed340bf2629ee1.png)
Looting iii[post sequence traversal and backtracking + dynamic planning]

Helm deploy etcd cluster

Computer network: sorting out common network interview questions (I)

Cereals Mall - Distributed Advanced p129~p339 (end)
随机推荐
[matlab] Simulink the input and output variables of the same module cannot have the same name
安装Mysql报错:Could not create or access the registry key needed for the...
数学知识——高斯消元(初等行变换解方程组)代码实现
tensorflow和torch代码验证cuda是否安装成功
Test technology stack arrangement -- self cultivation of test development engineers
short i =1; I=i+1 and short i=1; Difference of i+=1
Documents to be used in IC design process
Graffiti intelligence is listed on the dual main board in Hong Kong: market value of 11.2 billion Hong Kong, with an annual revenue of 300 million US dollars
A method of removing text blur based on pixel repair
Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
Multithreading Basics: basic concepts of threads and creation of threads
How word displays modification traces
五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
Unlock 2 live broadcast themes in advance! Today, I will teach you how to complete software package integration Issues 29-30
Detailed idea and code implementation of infix expression to suffix expression
Computer network: sorting out common network interview questions (I)
R language uses rchisq function to generate random numbers that conform to Chi square distribution, and uses plot function to visualize random numbers that conform to Chi square distribution
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
三面蚂蚁金服成功拿到offer,Android开发社招面试经验