[LC] 42. Trapping Rain Water
2024-10-08 19:28:08
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
Example:
Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
class Solution(object):
def trap(self, height):
"""
:type height: List[int]
:rtype: int
"""
left, right = 0, len(height) - 1
leftMax, rightMax, res = 0, 0, 0
while left < right:
if height[left] <= height[right]:
leftMax = max(leftMax, height[left])
res += leftMax - height[left]
left += 1
else:
rightMax = max(rightMax, height[right])
res += rightMax - height[right]
right -= 1
return res
最新文章
- 4、项目的培训 - PMO项目管理办公室
- div跟随页面滚动
- python学习之路-day6-面向对象
- Java中的Comparable接口和Comparator接口
- Windows Live Writer离线编写博客
- 将XML文件中的内容转换为Json对象
- c语言字符串大小写字母转换
- ios运行某些工程时屏幕上下出现黑边的解决办法
- windows核心编程-信号量(semaphore)
- C语言链表各类操作详解
- JavaScript中的setAttribute用法
- 搜狗2015校园招聘javaproject师面经
- virus.win32.parite.H病毒的查杀方法
- CodeForces 591B Rebranding
- JS的事件动态绑定机制
- Openstack Swift 原理、架构与 API 介绍
- 渗透测试,form对象类型转换,简单demo
- Go中多个返回值的技巧
- HTTP中Post与Put的区别
- python jieba库
热门文章
- [CF百场计划]#2 Codeforces Round #618 (Div. 2)
- MyBatis从入门到精通(第6章):6.3 使用枚举或其他对象
- python装饰器的参数传递
- MySQL学习笔记——〇三 MySQL习题
- sybase连接失败 JZ006: Caught IOException: java.net.ConnectException处理方式
- request请求生命周期
- pandas 学习笔记【持续更新】
- iOS 直接使用16进制颜色
- 17.3.15---C语言详解FILE文件操作
- UML-各阶段如何编写用例