当前位置:网站首页>接雨水问题解析
接雨水问题解析
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
边栏推荐
- Don't miss this underestimated movie because of controversy!
- English topic assignment (25)
- C # use Marshall to manually create unmanaged memory in the heap and use
- [matlab] Simulink the input and output variables of the same module cannot have the same name
- How to type multiple spaces when editing CSDN articles
- [paper notes] transunet: transformers make strongencoders for medical image segmentation
- The list of people who passed the fifth phase of personal ability certification assessment was published
- GCC [7] - compilation checks the declaration of functions, and link checks the definition bugs of functions
- Simple understanding of MySQL database
- 思维导图+源代码+笔记+项目,字节跳动+京东+360+网易面试题整理
猜你喜欢

零基础入门PolarDB-X:搭建高可用系统并联动数据大屏

Based on butterfly species recognition

Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
![Looting iii[post sequence traversal and backtracking + dynamic planning]](/img/9b/e9eeed138e46afdeed340bf2629ee1.png)
Looting iii[post sequence traversal and backtracking + dynamic planning]

Word如何显示修改痕迹

Analysis of frequent chain breaks in applications using Druid connection pools

Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
深入分析,Android面试真题解析火爆全网
三年Android开发,2022疫情期间八家大厂的Android面试经历和真题整理

An error occurs when installing MySQL: could not create or access the registry key needed for the
随机推荐
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
A method of removing text blur based on pixel repair
Use map function and split function to type multiple elements in one line
ROS自定义消息发布订阅示例
The second day of rhcsa study
渲大师携手向日葵,远控赋能云渲染及GPU算力服务
R语言ggplot2可视化:使用ggpubr包的ggdotplot函数可视化点阵图(dot plot)、设置palette参数设置不同水平点阵图数据点和箱图的颜色
Translation D28 (with AC code POJ 26:the nearest number)
Use of map (the data of the list is assigned to the form, and the JSON comma separated display assignment)
Php+redis realizes the function of canceling orders over time
Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
[paper notes] transunet: transformers make strongencoders for medical image segmentation
A popular explanation will help you get started
First day of rhcsa study
[depth first search] Ji suanke: Square
CPU负载很低,loadavg很高处理方法
安装Mysql报错:Could not create or access the registry key needed for the...
About static type, dynamic type, ID, instancetype
五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”
Yutai micro rushes to the scientific innovation board: Huawei and Xiaomi fund are shareholders to raise 1.3 billion