题目来源


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. 理解cookie的path和domain属性

    今天在做验证码时发现一个问题:A.B窗口都打开同一个页面,A先生成一个验证码,B再生成验证码,这时A所生成的验证码被B覆盖掉了.原因是使用了同名的cookie来存储验证码.一时找不到解决方法就参考了W ...

  2. 【POJ】2828 Buy Tickets(线段树+特殊的技巧/splay)

    http://poj.org/problem?id=2828 一开始敲了个splay,直接模拟. tle了.. 常数太大.. 好吧,说是用线段树.. 而且思想很拽.. (貌似很久以前写过貌似的,,) ...

  3. 转:给自己TopCoder SRM的建议

    强哥写的. 原文链接 1. 是否需要 long long 2. 是否有 除0 的可能 3. 是否需要cmp特判精度 4. 是否必须使用double,能转化成整数运算么?怎么转换 5. 关键部分无序的问 ...

  4. NSCharacterSet 简单用法

    NSCharacterSet 简单用法 NSCharacterSet其实是许多字符或者数字或者符号的组合,在网络处理的时候会用到 NSMutableCharacterSet *base = [NSMu ...

  5. OpenCV 2.4.10 Linux Qt Conifguration

    Download CMake 2.8.12 Extract the file, and run "./bootstrap", then "make", then ...

  6. 如何把in_array 的第三个参数strict设置为 true

    var_dump(in_array(0, array('s' )); 这句话的结果是bool(true). 因为in_array会将0 和's' 进行比较,0是number类型,'s'是string类 ...

  7. 使用AuthDBMType指令支持多种类似DBM的数据库

    Unix线程 在支持POSIX线程的Unix系统上,现在Apache能在混合的多进程.多线程模式下运行,使很多(但非全部)配置的可伸缩性得到了改善. 新的编译系统 重写了编译系统,现在是基于autoc ...

  8. [排错] PATH_MISSING

    症状: 更换了磁带机部件,导致Robots选项卡中的TLD(2) Robotic Path显示为PATH_MISSING. 解决方法: 概括起来就是: 删除TLD(2),重新创建Robot记录,然后重 ...

  9. Ajax解决IE浏览器兼容问题

    ServletContext 被 Servlet 程序用来与 Web 容器通信.例如写日志,转发请求.每一个 Web 应用程序含有一个Context,被Web应用内的各个程序共享. 因为Context ...

  10. ThinkPHP 3.2.2 实现持久登录 ( 记住我 )

    实现持久登录,即用户在登录时,勾选了"记住我"之后,无论是否关闭浏览器,只要不退出登录,在指定的时间内始终保持登录状态(缺点是在另一台电脑上登录过后,之前那台电脑就不能继续保持登录 ...