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

最新文章

  1. 4、项目的培训 - PMO项目管理办公室
  2. div跟随页面滚动
  3. python学习之路-day6-面向对象
  4. Java中的Comparable接口和Comparator接口
  5. Windows Live Writer离线编写博客
  6. 将XML文件中的内容转换为Json对象
  7. c语言字符串大小写字母转换
  8. ios运行某些工程时屏幕上下出现黑边的解决办法
  9. windows核心编程-信号量(semaphore)
  10. C语言链表各类操作详解
  11. JavaScript中的setAttribute用法
  12. 搜狗2015校园招聘javaproject师面经
  13. virus.win32.parite.H病毒的查杀方法
  14. CodeForces 591B Rebranding
  15. JS的事件动态绑定机制
  16. Openstack Swift 原理、架构与 API 介绍
  17. 渗透测试,form对象类型转换,简单demo
  18. Go中多个返回值的技巧
  19. HTTP中Post与Put的区别
  20. python jieba库

热门文章

  1. [CF百场计划]#2 Codeforces Round #618 (Div. 2)
  2. MyBatis从入门到精通(第6章):6.3 使用枚举或其他对象
  3. python装饰器的参数传递
  4. MySQL学习笔记——〇三 MySQL习题
  5. sybase连接失败 JZ006: Caught IOException: java.net.ConnectException处理方式
  6. request请求生命周期
  7. pandas 学习笔记【持续更新】
  8. iOS 直接使用16进制颜色
  9. 17.3.15---C语言详解FILE文件操作
  10. UML-各阶段如何编写用例