当前位置:网站首页>接雨水问题解析
接雨水问题解析
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
边栏推荐
- 2022.2.12
- 反射及在运用过程中出现的IllegalAccessException异常
- 【论文笔记】TransUNet: Transformers Make StrongEncoders for Medical Image Segmentation
- Is not a drawable (color or path): the vector graph downloaded externally cannot be called when it is put into mipmap, and the calling error program crashes
- Php+redis realizes the function of canceling orders over time
- How word displays modification traces
- 包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
- Simple understanding of MySQL database
- [depth first search] Ji suanke: a joke of replacement
- Reflection and illegalaccessexception exception during application
猜你喜欢
Three years of Android development, Android interview experience and real questions sorting of eight major manufacturers during the 2022 epidemic
ROS自定义消息发布订阅示例
[depth first search] Ji suanke: find numbers
Documents to be used in IC design process
Characteristic colleges and universities, jointly build Netease Industrial College
A popular explanation will help you get started
第五期个人能力认证考核通过名单公布
Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
打家劫舍III[后序遍历与回溯+动态规划]
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
随机推荐
第五期个人能力认证考核通过名单公布
谷粒商城--分布式高级篇P129~P339(完结)
R语言使用dt函数生成t分布密度函数数据、使用plot函数可视化t分布密度函数数据(t Distribution)
受益匪浅,安卓面试问题
spark基础-scala
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
Reptiles have a good time. Are you full? These three bottom lines must not be touched!
Black Horse - - Redis Chapter
Three years of Android development, Android interview experience and real questions sorting of eight major manufacturers during the 2022 epidemic
Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
About static type, dynamic type, ID, instancetype
Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up
ACTF 2022圆满落幕,0ops战队二连冠!!
JDBC详解
IC设计流程中需要使用到的文件
Qlabel marquee text display
ROS自定义消息发布订阅示例
CCNP Part 11 BGP (III) (essence)
渲大师携手向日葵,远控赋能云渲染及GPU算力服务
冒烟测试怎么做