题目来源


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. [Unity2D]脚本基类MonoBehaviour介绍

    Unity中的脚本都是继承自MonoBehaviour. MonoBehaviour 表示一个单一的行为.Unity中用户对游戏对象的操作被分割成若干个单一行为.每个单一行为都作为一个MonoBeha ...

  2. overload和override的区别(转)

    overload和override的区别 override(重写) 1.方法名.参数.返回值相同.2.子类方法不能缩小父类方法的访问权限.3.子类方法不能抛出比父类方法更多的异常(但子类方法可以不抛出 ...

  3. CruiseControl.Net学习记录

    一.下载 官网 二.安装 本文版本:1.8.5.0 运行安装程序,按照提示"下一步”,直到完成即可. 安装完毕之后, 生成一个windows服务,CruiseControl.NET Serv ...

  4. 【转】个人对JQuery Proxy()函数的理解

    原文地址:http://www.cnblogs.com/acles/archive/2012/11/20/2779282.html JQuery.proxy(function,context): 使用 ...

  5. centos7 安装及配置

    第一步 下载centoshttps://www.centos.org/download/CentOS-7.0-1406-x86_64-DVD.iso:这个镜像(DVD image)包括了那些可以用安装 ...

  6. Swing 刷新容器

    JPanel pchks = new JPanel();// 容器刷新(重新layout所有空间)pchks.validate();// 容器重绘(当容器内的东西由多变少时,防止多出来的部分没有清楚) ...

  7. css修改,类似elememt.style样式修改

    使用!important 语法优先权. .yui-b { margin-left:0px ! important; }

  8. 【转载】在LoadRunner向远程Linux/Unix执行命令行并收集性能数据

    前面介绍过在LoadRunner的Java协议实现“使用SSH连接Linux”,当然连接之后的故事由你主导. 今天要讲的,是一个非Java版本.是对“在LoadRunner中执行命令行程序之:pope ...

  9. the core or essence of a computer

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The ALU is that part ...

  10. Checklist For Choosing The Right Database Engine

    http://sqlite.org/whentouse.html Appropriate Uses For SQLite SQLite is not directly comparable to cl ...