[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 ...
随机推荐
- c# 纯代码方式创建快捷方式
using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; using Syst ...
- POJ 3277 City Horizon(扫描线+线段树)
题目链接 类似求面积并..2Y.. #include <cstdio> #include <cstring> #include <string> #include ...
- BZOJ3772: 精神污染
Description 兵库县位于日本列岛的中央位置,北临日本海,南面濑户内海直通太平洋,中央部位是森林和山地,与拥有关西机场的大阪府比邻而居,是关西地区面积最大的县,是集经济和文化于一体的一大地区, ...
- PCL 1.4.0 VS 2010 Configuration
Open VS2010, create a new project, then open Property Manager, double-click Microsoft.Cpp.win32.user ...
- C# - Dbhelp
dbhelp.cs using System; using System.Data; using System.Data.Common; using System.Configuration; pub ...
- [转]支付宝接口程序、文档及解读(ASP.NET)
本文转自:http://www.cnblogs.com/blodfox777/archive/2009/11/03/1595223.html 最近需要为网站加入支付宝的充值接口,而目前关于支付宝接口开 ...
- CSS权威指南 - 浮动和定位 2
定位 定位的想法很简单元素框相对于正常位置出现在哪里. 定位:static,相对, 绝对, fixed, 继承 static就是默认的位置 相对就是相对于默认位置的偏移.原来的static定位位置依然 ...
- MySQL 创建用户 与 授权
例,需要给 121.52.215.100 连接添加一个用户 dee,密码是 123456,他只能对数据库 vshop 有 select 权限: CREATE USER '; GRANT SELECT ...
- PHP 错误与异常 笔记与总结(6)将错误日志保存在系统日志中
[将错误记录到系统日志中] 在 php.ini 中将 error_log 设置为: error_log = syslog 或者在运行时使用 ini_set() 函数设置. [例1] <?php ...
- MySQL 命令行导出、导入Select 查询结果
<!-- 环境: Windows 2003 SP2 + MySQL5.5.28 Author: 博客园小dee --> 有的时候需要把在一张表中用 select 语句查询出来的结果保存到另 ...