当前位置:网站首页>接雨水问题解析
接雨水问题解析
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
边栏推荐
- Translation D28 (with AC code POJ 26:the nearest number)
- Take a look at how cabloyjs workflow engine implements activiti boundary events
- 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
- R language uses DT function to generate t-distribution density function data and plot function to visualize t-distribution density function data
- MRO工业品企业采购系统:如何精细化采购协同管理?想要升级的工业品企业必看!
- Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
- Elastic search indexes are often deleted [closed] - elastic search indexes gets deleted frequently [closed]
- 打家劫舍III[后序遍历与回溯+动态规划]
- A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
- Interview assault 63: how to remove duplication in MySQL?
猜你喜欢

关于静态类型、动态类型、id、instancetype
![[depth first search] Ji suanke: Square](/img/fc/e42ae0d036be258bed5623d55fc2db.jpg)
[depth first search] Ji suanke: Square
![[paper notes] transunet: transformers make strongencoders for medical image segmentation](/img/21/3d4710024248b62495e2681ebd1bc4.png)
[paper notes] transunet: transformers make strongencoders for medical image segmentation

Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry

CCNP Part 11 BGP (III) (essence)

Analysis of frequent chain breaks in applications using Druid connection pools

渲大师携手向日葵,远控赋能云渲染及GPU算力服务

Interface test tool - postman

php+redis实现超时取消订单功能

The second day of rhcsa study
随机推荐
Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
黑馬--Redis篇
pytorch常见损失函数
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
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
GCC【7】- 编译检查的是函数的声明,链接检查的是函数的定义bug
ROS custom message publishing subscription example
spark基础-scala
C # use Marshall to manually create unmanaged memory in the heap and use
通俗的讲解,带你入门协程
In 50W, what have I done right?
QLabel 跑马灯文字显示
Camel case with Hungarian notation
Leetcode topic [array] - 119 Yang Hui triangle II
Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
Computer network: sorting out common network interview questions (I)
[paper notes] transunet: transformers make strongencoders for medical image segmentation
When visual studio code starts, it prompts "the code installation seems to be corrupt. Please reinstall." Solution to displaying "unsupported" information in the title bar
short i =1; i=i+1与short i=1; i+=1的区别
Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers