[LeetCode]题解(python):042-Trapping Rain Water
题目来源
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的更多相关文章
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- LeetCode 042 Trapping Rain Water
题目要求:Trapping Rain Water Given n non-negative integers representing an elevation map where the width ...
- 【LeetCode】042 Trapping Rain Water
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- leetcode 第41题 Trapping Rain Water
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- LeetCode(42)Trapping Rain Water
题目 Given n non-negative integers representing an elevation map where the width of each bar is 1, com ...
- [leetcode][042] Trapping Rain Water (Java)
我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目在这里: ...
- 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 ...
- LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]
题目:Given n non-negative integers representing an elevation map where the width of each bar is 1, com ...
- 042 Trapping Rain Water 接雨水
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算下雨之后能接多少雨水.例如,输入 [0,1,0,2,1,0,1,3,2,1,2,1],返回 6. 详见:https://leetcode.c ...
- LeetCode: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...
随机推荐
- css expression
概述 css expression(css表达式)又称Dynamic properties(动态属性)是早期微软DHTML的产物,以其可以在Css中定义表达式(公式)来达到建立元素间属性之间的联系等作 ...
- python 根据对象和方法名,返回提供这个方法的定义的类
def find_defining_class(obj, method_name): for ty in type(obj).mro(): if method_name in ty.__dict__: ...
- Js作用域与作用域链详解
一直对Js的作用域有点迷糊,今天偶然读到Javascript权威指南,立马被吸引住了,写的真不错.我看的是第六版本,相当的厚,大概1000多页,Js博大精深,要熟悉精通需要大毅力大功夫. 一:函数作用 ...
- COJ885 LCS???
试题描述 输入两个字符串A.B,输出他们的最长连续公共子串长度. 输入 第一行为一个字符串A. 第二行为一个字符串B. 输出 输出他们的最长连续公共子串长度. 输入示例 ababab bababbab ...
- mysql用户权限
mysql> show grants for root@'localhost';+-------------------------------------------------------- ...
- MUI - 预加载
预加载都是在mui.init({)}中执行的 方式一:preload一次仅能预加载一个页面(除非循环) var subWebview = mui.preload({ url: 'examples/ac ...
- [转]C#开发ActiveX控件,.NET开发OCX控件案例
引自:百度 http://hi.baidu.com/yanzuoguang/blog/item/fe11974edf52873aaec3ab42.html 讲下什么是ActiveX控件,到底有什么 ...
- IOS第六天(3:scrollView 图片轮播器)
IOS第六天(3:scrollView 图片轮播器) #import "HMViewController.h" #define kImageCount 5 @interface H ...
- 【液晶模块系列基础视频】4.2.X-GUI图形界面库-画矩形函数简介
[液晶模块系列基础视频]4.2.X-GUI图形界面库-画矩形函数简介 ============================== 技术论坛:http://www.eeschool.org 博客地址: ...
- 暑假训练round2 D: 好序列(Manacher)
Problem 1061: 好序列 Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit interger IO format: %lld ...