# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Water
https://oj.leetcode.com/problems/trapping-rain-water/ 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.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. ===Comments by Dabay===
请参考:http://blog.csdn.net/wzy_1988/article/details/17752809
挨个分析每个A[i]能trapped water的容量,然后将所有的A[i]的trapped water容量相加即可
其次,对于每个A[i]能trapped water的容量,取决于A[i]左右两边的高度(可延展)较小值与A[i]的差值,
即volume[i] = [min(left[i], right[i]) - A[i]] * 1,这里的1是宽度,如果the width of each bar is 2,那就要乘以2了
''' class Solution:
# @param A, a list of integers
# @return an integer
def trap(self, A):
if len(A) <= 2:
return 0 highest_on_left = [A[0] for _ in A]
for i in xrange(1, len(A)):
highest_on_left[i] = max(highest_on_left[i-1], A[i]) highest_on_right = [A[-1] for _ in A]
for i in xrange(len(A)-2, -1, -1):
highest_on_right[i] = max(highest_on_right[i+1], A[i]) res = 0
for i in xrange(1, len(A)-1):
res += min(highest_on_left[i], highest_on_right[i]) - A[i]
return res def main():
s = Solution()
nums = [0,1,0,2,1,0,1,3,2,1,2,1]
print s.trap(nums) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[Leetcode][Python]42: Trapping Rain Water的更多相关文章

  1. 【LeetCode】42. Trapping Rain Water

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

  2. leetcode problem 42 -- Trapping Rain Water

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

  3. 【一天一道LeetCode】#42. Trapping Rain Water

    一天一道LeetCode系列 (一)题目 Given n non-negative integers representing an elevation map where the width of ...

  4. 【LeetCode】42. Trapping Rain Water 接雨水 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...

  5. LeetCode OJ 42. Trapping Rain Water

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

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

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

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

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

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

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

  9. LeetCode - 42. Trapping Rain Water

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

随机推荐

  1. SqlServer sys.partition_view

    --查看partition的四个视图 select * from sys.partition_functions--查看分区函数 select * from sys.partition_paramet ...

  2. HttpClient工具类

    HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议. ...

  3. svn add后的数据如何取消-svn revert??--zz

    svn add后的数据如何取消-svn revert?? 有时候你发现svn add后,这个提交的数据又不需要了.这时候需要有svn revert来处理了. 原文链接:http://hi.baidu. ...

  4. [转]UltraISO制作U盘启动盘安装Win7/9/10系统攻略

    原文地址:http://www.cnblogs.com/pchmonster/p/4716708.html U盘安装好处就是不用使用笨拙的光盘,光盘还容易出现问题,无法读取的问题.U盘体积小,携带方便 ...

  5. Notepad++背景颜色设置

    经常试用notepad++看代码,白色的背景连续看的时间长了眼睛很容变花,所以找了相关的设置选项,分享给大家 具体设置步骤如下: 然后如下设置 这样前景色背景色已经发生改变了哟,下面再修改下选中行的背 ...

  6. hdu 4930 Fighting the Landlords--2014 Multi-University Training Contest 6

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4930 Fighting the Landlords Time Limit: 2000/1000 MS ...

  7. java学习笔记day03

    1.二维数组,即一维护 int[][] arr1 = new int[3][2]; int[][] arr2 ={{2,4,3,6,22,7},{3,6,8,9},{10,13,24,5}}; pub ...

  8. Tomcat 的三种(bio,nio.apr) 高级 Connector 运行模式

    tomcat的运行模式有3种.修改他们的运行模式.3种模式的运行是否成功,可以看他的启动控制台,或者启动日志.或者登录他们的默认页面http://localhost:8080/查看其中的服务器状态. ...

  9. Android设置背景

    一.设置图片背景 首先你先将一个的背景图片存入工程中res/drawble(当然drawble-hdpi.drawble-mdpi.drawble-ldpi中一个或者几个文件夹都可)文件夹中.假如我存 ...

  10. java面对对象 关键字this super

    this:this是指向对象本身的一个指针,成员函数内部指向当前类的对象 其实this主要要三种用法: 1.表示对当前对象的引用! 2.表示用类的成员变量,而非函数参数,注意在函数参数和成员变量同名是 ...