题目来源


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 042 Trapping Rain Water

    题目要求:Trapping Rain Water Given n non-negative integers representing an elevation map where the width ...

  3. 【LeetCode】042 Trapping Rain Water

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

  4. leetcode 第41题 Trapping Rain Water

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

  5. LeetCode(42)Trapping Rain Water

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

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

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

  7. 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 ...

  8. LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]

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

  9. 042 Trapping Rain Water 接雨水

    给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算下雨之后能接多少雨水.例如,输入 [0,1,0,2,1,0,1,3,2,1,2,1],返回 6. 详见:https://leetcode.c ...

  10. LeetCode: Trapping Rain Water 解题报告

    https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...

随机推荐

  1. Windows Phone 硬件检测

    private static bool IsWvga{ get { return App.Current.Host.Content.ScaleFactor == 100; }} private sta ...

  2. 【BZOJ】3339: Rmq Problem & 3585: mex(线段树+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3585 好神的题. 但是!!!!!!!!!!!!!!我线段树现在要开8倍空间才能过!!!!!!!!!! ...

  3. 获取枚举类型Description特性的描述信息

    C#中可以对枚举类型用Description特性描述. 如果需要对Description信息获取,那么可以定义一个扩展方法来实现.代码如下: public static class EnumExten ...

  4. hdu 饭卡

    本题的思路是:首先如果m<5,直接输出:若m>5,则先拿出5元钱买最贵的东西,这样背包容量就变成了m-5,商品数量为n-1的0/1背包问题. 此题的状态转移方程为:dp[j]=max{dp ...

  5. Phaser提供了Button对象简单的实现一个按钮

    Phaser是一个简单易用且功能强大的html5游戏框架,利用它可以很轻松的开发出一个html5游戏.在这篇文章中我就教大家如何用Phaser来制作一个前段时间很火爆的游戏:Flappy Bird,希 ...

  6. 下载站中的下载连接其实是php脚本文件控制

    什么是php文件,PHP是一种服务器端HTML-嵌入式脚本描述语言. 其最强大和最重要的特征是其数据库集成层,使用它完成一个含有数据库功能的网页是不可置信的简单.在HTML文件中, PHP脚本程序(语 ...

  7. java数组排序问题:array.sort()是从小到大排序,那么如何从大到小排序?

    别告诉我从i=a.length开始打印然后i--!因为数组没变啊,只是打印顺序变了.有木有啥别的方法,除了冒泡插入选择.. nteger [] array=new Integer[]{1,2,3,4, ...

  8. PHP 多个文件上传

    关键函数: is_uploaded_file():用于判断指定的文件是否是通过 HTTP POST 上传的,如果是则返回 TRUE.用于防止潜在的攻击者对原本不能通过脚本交互的文件进行非法管理,这可以 ...

  9. mysql没有delete操作,那是delete from操作,

    1.mysql没有delete操作,那是delete from操作, 2.DELETE FROM table_name [WHERE Clause]

  10. Ubuntu中搭建Nginx+PHP环境最简单的方法

    先安装: sudo apt-get install nginx php5-fpm 然后编辑配置文件: sudo gedit /etc/nginx/site-available/default 找到lo ...