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.

The above elevation map is represented by
array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water
(blue section) are being trapped. Thanks Marcos for contributing this image!

一看这道题太简单就没自己写了,代码中A=height是为了能直接用别人的代码。

转自:http://m.blog.csdn.net/blog/nerv3x3/37339357

思路:

将一个数列看成是一堆高低不一的蓄水墙,数值即这面墙的高度,求总的蓄水最大量。其实题目中的图就已经一目了然了,无需过多文字说明。
针对某一个x点,思考决定其蓄水量应该是该点左右两边分别出现过的最高的墙中的最小者决定的。例如,针对A=[0,1,0,2,1,0,1,3,2,1,2,1]这个例子,A[4]=1这一点,其左边出现过的最高墙是A[3]=2,其右边出现过的最高墙是A[7]=3,因此两者中的最小者就就是A[3]=2,因此A[4]的蓄水高度就是A[3]-A[4]=1,乘以底边1,即A[4]蓄水量为1。
针对每一个点去单独寻找这个最小值显然是不合理的,这样时间复杂度就是O(n*n)了。申请一个与原数组A等长的数组max_heights用来记录每个点的最大蓄水量,首先从左至右遍历一次A,记录当前遇到过的最大值,依次填入到max_heights中。然后再从右至左遍历一次A,也记录当前遇到过的最大值,与max_heights中对应位置的值进行比较,如果比max_heights中对应的值小,则替换掉max_heights中的值。通过这种方法,就能用2次时间复杂度为O(n)的遍历找出每个点的蓄水量,然后遍历max_heights,如果比A中对应的数值大的话,就累加其差值,最后得到的就是蓄水总量。也就是说,如果A中数值对max_heights中对应数值大,说明这个点无法蓄水。
空间复杂度为O(n),时间复杂度为O(n)。

class Solution:
# @param {integer[]} height
# @return {integer}
def trap(self, height):
A=height
len_A = len(A)
if 1 == len_A:
return 0
max_heights = [0] * len_A
left_max = 0
for i in range(0, len_A):
if A[i] > left_max:
left_max = A[i]
max_heights[i] = left_max
right_max = 0
for i in range(len_A - 1, -1, -1):
if A[i] > right_max:
right_max = A[i]
if right_max < max_heights[i]:
max_heights[i] = right_max
result = 0
for i in range(0, len_A):
if max_heights[i] > A[i]:
result += (max_heights[i] - A[i])
return result

Trapping Rain Water——经典的双边扫描问题的更多相关文章

  1. [LeetCode] Trapping Rain Water 收集雨水

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

  2. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  3. LeetCode: Trapping Rain Water 解题报告

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

  4. [LeetCode] 42. Trapping Rain Water 收集雨水

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

  5. [LeetCode] Trapping Rain Water II 收集雨水之二

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  6. [LintCode] Trapping Rain Water 收集雨水

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

  7. LeetCode - 42. Trapping Rain Water

    42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...

  8. 有意思的数学题:Trapping Rain Water

    LeetCode传送门 https://leetcode.com/problems/trapping-rain-water/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的 ...

  9. [Leetcode][Python]42: Trapping Rain Water

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...

随机推荐

  1. 微信小程序 事件绑定 bind和catch 区别

    转自:https://blog.csdn.net/xiaoqiang_0719/article/details/79729592 本文以冒泡事件tap(手指触摸后马上离开,也就是点击事件)为例子来区别 ...

  2. Spring @Async的异常处理

    楼主在前面的2篇文章中,分别介绍了Java子线程中通用的异常处理,以及Spring web应用中的异常处理.链接如下: Java子线程中的异常处理(通用) Spring web引用中的异常处理 今天, ...

  3. Centos7下关于memcached的安装和简单使用

    在这里,由于用编译安装memcached服务端过于复杂,因此我选用依赖管理工具 yum 来实现 memcached 的服务端安装: [root@localhost /]# yum install -y ...

  4. 【bzoj2038-小z的袜子】莫队算法

    莫队例题. 莫队学习:https://www.cnblogs.com/Paul-Guderian/p/6933799.html 本题 分子是sigma(c(sum[a[i]],2)),分母是sigma ...

  5. 【LibreOJ】#539. 「LibreOJ NOIP Round #1」旅游路线

    [题意]给定正边权有向图,车油量上限C,每个点可以花费pi加油至min(C,ci),走一条边油-1,T次询问s点出发带钱q,旅行路程至少为d的最多剩余钱数. n<=100,m<=1000, ...

  6. 【BZOJ】1577: [Usaco2009 Feb]庙会捷运Fair Shuttle

    [题意]公车从1开到n,有k群牛想从一个点到达另一个点,公车最多乘坐c个人,牛群可以拆散,问最多载多少牛到达目的地. [算法]贪心+堆 [题解]线段和点的贪心,一般有按左端点排序和按右端点排序两种方法 ...

  7. bootstrap框架的搭建

    bootstrap框架 Bootstrap,来自 Twitter,是目前最受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 Web 开发更加快 ...

  8. Farey Sequence (欧拉函数+前缀和)

    题目链接:http://poj.org/problem?id=2478 Description The Farey Sequence Fn for any integer n with n >= ...

  9. HDU 1205 吃糖果 (数学)

    题目链接 Problem Description HOHO,终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢先吃一种,下一次吃 ...

  10. python初步学习-python数据类型-集合(set)

    集合 在已经学过的数据类型中: 能够索引的,如list/str,其中的元素可以重复 可变的,如list/dict,即其中的元素/键值对可以原地修改 不可变的,如str/int,即不能进行原地修改 无索 ...