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

[LC] 42. Trapping Rain Water的更多相关文章

  1. LeetCode - 42. Trapping Rain Water

    42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...

  2. [Leetcode][Python]42: Trapping Rain Water

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...

  3. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

  4. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

  5. LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))

    LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...

  6. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  7. 刷题42. Trapping Rain Water

    一.题目说明 题目是42. Trapping Rain Water,翻译起来就是"接雨水".给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水.题目难度是Hard 二.我的 ...

  8. [LeetCode] 42. Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  9. 【LeetCode】42. Trapping Rain Water

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

随机推荐

  1. IP首部检验和的计算和举例

    IP首部校验和 首部校验和(16位)字段只检验数据报的首部,不检验数据部分.这里不采用CRC检验码而采用简单的计算方法. 发送端 首先将检验和置零,求首部数据的补码和(包含检验和),因为为零,所以无影 ...

  2. 全天候式投资组合(All-weather Portfolio)

    此策略是美国知名对冲基金Bridgewater的负责人Ray Dalio长期研究的成果,其核心观点是将宏观因子,经济情景(economic scenario),和上文中提到的等风险权重(risk pa ...

  3. Iptables的规则语法

    Iptables的规则语法 分类: 防火墙2012-04-19 17:09 1228人阅读 评论(0) 收藏 举报 inputtcpfilter防火墙output网络 (一) 基本语法 iptable ...

  4. h5-语义化标签的兼容性问题

    1.html代码 <header>头</header> <nav>导航栏</nav> <main> <article>左< ...

  5. os简介

    1. 操作系统(Operation System,OS) 操作系统作为接口的示意图  没有安装操作系统的计算机,通常被称为 裸机 如果想在 裸机 上运行自己所编写的程序,就必须用机器语言书写程序 如 ...

  6. UVA 10269 Super Mario,最短路+动态规划

    这个题目我昨晚看到的,没什么思路,因为马里奥有boot加速器,只要中间没有城堡,即可不耗时间和脚力,瞬间移动不超过L距离,遇见城堡就要停下来,当然不能该使用超过K次...我纠结了很久,最终觉得还是只能 ...

  7. \_\_module\_\_和\_\_class\_\_

    目录 __module__和__class__ 一.__module__ 二.通过字符导入模块 三.__class__ __module__和__class__ # lib/aa.py class C ...

  8. Django学习---多人博客项目(1)

    一.创建项目和应用 ​ 在Pycharm中用Django模板创建一个工程文件 创建项目 python manage.py startproject 项目名 . 创建应用 python manage.p ...

  9. CF 1096D Easy Problem [动态规划]

    题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 有一长度为n的字符串,每一字符都有一个权值,要求现在从中取出若干个字符,使得字符串中没 ...

  10. shell手册

    摘自雪松同学 0说明{ # shell实例手册最新下载地址: http://hi.baidu.com/quanzhou722/item/f4a4f3c9eb37f02d46d5c0d9 # pytho ...