题目来源


https://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.


题意分析


Input: the height of the bars as list

Output: the volumn of the water the bars can save

Conditions:注意到因为bar的高度不一样,所以中间的bar可以容纳一定量的水,题意就是求容纳的水的大小


题目思路

注意到每一个bar所能容纳的水的量,就是其左右最高的bar中的较小值减去该bar的高度,所以先建立一个leftMostHigh的list记录每一个bar之前的最高bar值,然后从后往前遍历,一边更新右边最高的bar值,同时计算每一个bar所能容纳的水量


AC代码(Python)


 _author_ = "YE"
 # -*- coding:utf-8 -*-

 class Solution(object):
     def trap(self, height):
         """
         :type height: List[int]
         :rtype: int
         """
         l = len(height)
         leftMostHigh = [0 for i in range(len(height))]
         leftmax = 0
         for i in range(l):
             leftMostHigh[i] = leftmax
             if height[i] > leftmax:
                 leftmax = height[i]

         rightmax = 0
         sum = 0
         for i in reversed(range(l)):
             if min(rightmax, leftMostHigh[i]) > height[i]:
                 sum = sum + min(rightmax, leftMostHigh[i]) - height[i]
             if height[i] > rightmax:
                 rightmax = height[i]

         return sum

 height = [0,1,0,2,1,0,1,3,2,1,2,1]
 s = Solution()
 print(s.trap(height))

[LeetCode]题解(python):042-Trapping Rain Water的更多相关文章

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

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

  2. leetcode 第41题 Trapping Rain Water

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

  3. [leetcode][042] Trapping Rain Water (Java)

    我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目在这里: ...

  4. Java for LeetCode 042 Trapping Rain Water

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

  5. [LeetCode] Trapping Rain Water II 收集雨水之二

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  6. [LeetCode] Trapping Rain Water 收集雨水

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

  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:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  10. LeetCode - 42. Trapping Rain Water

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

随机推荐

  1. java 中如何声明线程安全的集合 set, map 和list

    线程安全的集合 http://blog.sina.com.cn/s/blog_508938e10102v1ig.html //make thread-safe list List MyStrList ...

  2. jvm 原理和优化

    在csdn 上看到的,觉得很好,收藏了: 原博文地址: 濤子 http://blog.csdn.net/ning109314/article/details/10411495/ JVM工作原理和特点主 ...

  3. js在head里插入style样式

    代码如下: var nod = document.createElement('style'), str = 'body{background:#000;color:#fff} a{color:#ff ...

  4. HDU 1535 Invitation Cards(最短路 spfa)

    题目链接: 传送门 Invitation Cards Time Limit: 5000MS     Memory Limit: 32768 K Description In the age of te ...

  5. 了解 JavaScript (5)– 翻转器(rollover)

    用 JavaScript 最常用的效果就是,当用户将鼠标移动到图片上时,会改变网页上的图像,这样页面就能对用户的操作及时作出反应,这种称为 翻转器(rollover)效果很容易实现,而且有很多应用场合 ...

  6. 让JS写的更接近OOP

    下面这段代码就是利用JS原型对象,来实现的类的继承DEMO $ 为jquery对象 ////公共方法 // $.oop.newClass=function newClass(obj) { // fun ...

  7. 双系统如何删除Linux,恢复Windows从MBR引导启动?

    嗯,现在愿意尝试Linux的人越来越多了.通常,如果一台电脑里已经装有了Windows,再装Linux,安装时,Linux的grub引导程 序就会覆盖掉保存在MBR当中的原来的Windows引导程序. ...

  8. JS 之继承

    ECMAScript继承是通过原型链来继承的.基本思想是利用原型来让一个引用类型继承另一个引用类型的属性和方法,使原型变为另一个对象的实例.通过原型链实现继承时,不能使用对象字面量创建原型方法,避免重 ...

  9. Java for LeetCode 038 Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...

  10. Extjs中TextField中显示图标

    现看实现效果:   实现代码: { width:500, height : 25, fieldLabel : '过滤条件', itemId : 'queryText', labelAlign : 'r ...