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.

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——经典的双边扫描问题的更多相关文章
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- LeetCode: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [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 ...
- [LintCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- 有意思的数学题:Trapping Rain Water
LeetCode传送门 https://leetcode.com/problems/trapping-rain-water/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的 ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
随机推荐
- 我们自己写的solr查询的代码作为search项目中的dao
我们自己写的solr查询的代码作为search项目中的dao,但是启动时会报错: 其实就是说 searchServiceImpl 中我们 Autowired 的 SearchDao 类 spring ...
- JS判断内容为空方法总结
HTML代码: 用户名:<input type="text" id="username"> <p style="color:red& ...
- git如何删除本地所有未提交的更改
stash很好用,至少不会影响 .gitignore 里面的不跟踪的文件: git add . && git stash && git stash drop ===== ...
- 基于http的追加协议、构建web内容的技术、web的攻击技术(9,10,11)
第九章 基于http的追加协议 用来提升http的瓶颈,比如Ajax技术,spdy等 第十章 构建web内容的技术 html.css.js等 第十一章 web的攻击技术 比如sql注入攻击.xss等.
- UIPageControl---iOS-Apple苹果官方文档翻译
本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/Ch ...
- 【洛谷 P4180】【模板】严格次小生成树[BJWC2010](倍增)
题目链接 题意如题. 这题作为我们KS图论的T4,我直接打了个很暴力的暴力,骗了20分.. 当然,我们KS里的数据范围远不及这题. 这题我debug了整整一个晚上还没debug出来,第二天早上眼前一亮 ...
- 如何升级nodejs版本 安装n模块报错 npm ERR! notsup Unsupported platform
如何升级nodejs版本 首先安装n模块, 输入npm install -g n n模块专门用来管理nodejs的版本. 如果出现npm ERR! notsup Unsupported platfor ...
- bootstrap基本用法
进入中文官网:http://www.bootcss.com 开始第一个Demo 准备工作: (1)进入bootstrap中文官网,点击起步 (2)下载生产环境 下载好的文件是一 ...
- ES6新特性学习(一)
一.什么是ES6 ECMAScript和JavaScript的关系 ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准,已经在2015年6月正式发布了.Mozilla公司 ...
- 4.0docker部署
设置容器的端口映射 -P :容器暴露的所有端口映射 -p :指定映射容器暴露的端口 Nginx部暑流程 docker run -p 80 --name web -t -i ubuntu /bin/b ...