当前位置:网站首页>接雨水问题解析
接雨水问题解析
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
边栏推荐
- 倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
- Wx applet learning notes day01
- In 50W, what have I done right?
- 深入分析,Android面试真题解析火爆全网
- Reptiles have a good time. Are you full? These three bottom lines must not be touched!
- The nearest library of Qinglong panel
- A popular explanation will help you get started
- 黑馬--Redis篇
- [depth first search] Ji suanke: a joke of replacement
- The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
猜你喜欢

Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast

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

Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries

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

Multithreading Basics: basic concepts of threads and creation of threads

如何提高网站权重

10 schemes to ensure interface data security

Detailed idea and code implementation of infix expression to suffix expression

Simple understanding of MySQL database

【论文笔记】TransUNet: Transformers Make StrongEncoders for Medical Image Segmentation
随机推荐
Tensorflow and torch code verify whether CUDA is successfully installed
Looting iii[post sequence traversal and backtracking + dynamic planning]
Modulenotfounderror: no module named 'PIL' solution
思维导图+源代码+笔记+项目,字节跳动+京东+360+网易面试题整理
Test technology stack arrangement -- self cultivation of test development engineers
[depth first search] Ji suanke: a joke of replacement
Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
Helm deploy etcd cluster
多线程基础:线程基本概念与线程的创建
Unlock 2 live broadcast themes in advance! Today, I will teach you how to complete software package integration Issues 29-30
Abstract classes and abstract methods
English topic assignment (25)
How word displays modification traces
Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go
反射及在运用过程中出现的IllegalAccessException异常
LeetCode-1279. 红绿灯路口
全套教学资料,阿里快手拼多多等7家大厂Android面试真题
In 50W, what have I done right?
pychrm社区版调用matplotlib.pyplot.imshow()函数图像不弹出的解决方法
JDBC details