接雨水

给出 n 个非负整数,代表一张X轴上每个区域宽度为 1 的海拔图, 计算这个海拔图最多能接住多少(面积)雨水。

如上图所示,海拔分别为 [0,1,0,2,1,0,1,3,2,1,2,1], 返回 6.

解题

先遍历一遍找到最高点,然后分别从两边开始,往最高点所在位置遍历,水位只会增高不会减小,且一直和最近遇到的最大高度持平,这样知道了实时水位,就可以边遍历边计算面积。

左到最高点,海拔高度下降的时候计算水位,左边最近比他高的海拔 减去 自己的海拔 ,这是该海拔的水位量。当遇到更高的海拔的时候更新海拔高度。

右到最高点,海拔高度下降的时候计算水位, 右边最近比他高的海拔 减去 自己的海拔 ,这是该海拔的水位量。当遇到更高的海拔的时候更新海拔高度。

 public class Solution {
/**
* @param heights: an array of integers
* @return: a integer
*/
public int trapRainWater(int[] A) {
// write your code here
int n = A.length;
if(n <= 2) return 0;
int max = -1, maxInd = 0;
int i = 0;
for(; i < n; ++i){
if(A[i] > max){
max = A[i];
maxInd = i;
}
}
int area = 0, root = A[0];
for(i = 0; i < maxInd; ++i){
if(root < A[i]) root = A[i];
else area += (root - A[i]);
}
for(i = n-1, root = A[n-1]; i > maxInd; --i){
if(root < A[i]) root = A[i];
else area += (root - A[i]);
}
return area;
}
}

Java Code

class Solution:
# @param heights: a list of integers
# @return: a integer
def trapRainWater(self, A):
# write your code here
n = len(A)
if(n <= 2):
return 0
max = -1
maxInd = 0
for i in range(n):
if A[i]> max:
max = A[i]
maxInd = i
leftMax = A[0]
area = 0
for i in range(maxInd):
if leftMax < A[i]:
leftMax = A[i]
else:
area = area + leftMax - A[i] rightMax = A[n-1]
for i in range(n-1,maxInd,-1):
if rightMax< A[i]:
rightMax = A[i]
else:
area = area + rightMax - A[i] return area

Python Code

lintcode:接雨水的更多相关文章

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

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

  2. lintcode算法周竞赛

    ------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...

  3. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

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

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

  5. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  6. Lintcode 85. 在二叉查找树中插入节点

    -------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...

  7. Lintcode 166. 主元素

    ----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...

  8. Lintcode 166. 链表倒数第n个节点

    ----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...

  9. Lintcode 157. 判断字符串是否没有重复字符

    ------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...

随机推荐

  1. Spark 3000门徒第二课scala面向对象总结

    昨晚听了王家林老师3000门徒spark系列课程的第二课,讲述了scala面向对象知识,并且带着过了一遍Spark核心类:SparkContent,RDD的代码,下面写一下心得: RDD是抽象类,实现 ...

  2. C#判断字符串为空

    string str = null; if (string.IsNullOrWhiteSpace(str)) { MessageBox.Show("字符串为null"); } if ...

  3. JAVA类与对象(九)------多态

    多态是同一个行为具有多个不同表现形式或形态的能力.多态性是对象多种表现形式的体现. 多态存在的三个必要条件: 继承 重写 父类引用指向子类对象 例:Parent p = new Child(); 当使 ...

  4. LintCode-BackPack II

    Given n items with size A[i] and value V[i], and a backpack with size m. What's the maximum value ca ...

  5. Phpstorm开发记

    Phpsotrm虽然付费项目,但网上有免费的激活码,也可以免费用不是. 1.首先是svn,windows项目下用Phpsotrm需要安装svn时,支付svn命令的,否则会提示找不到svn命令.2.建项 ...

  6. memcached使用说明

    1.在服务器上注册服务 2.启动服务:services.msc       3.客户端创建服务接口 object Get(string key); List<string> GetKeys ...

  7. 设计模式之组合模式(Composite)

    组合模式原理:组合模式的作用是讲继承同一父类的不同子类对象组合起来,形成一个树形的结构,例如公司的部门组织 代码如下 #include <iostream> #include <st ...

  8. UIResponder

    原网址:http://www.cnblogs.com/kuku/archive/2011/11/12/2246389.html 在 iOS 中,一个 UIResponder 对象表示一个可以接收触摸屏 ...

  9. Cross Validation done wrong

    Cross Validation done wrong Cross validation is an essential tool in statistical learning 1 to estim ...

  10. ASP.NET输出PNG图片时出现GDI+一般性错误的解决方法

    偶原来的用ASP.NET生成验证码图片时用的是JPG格式,今天想把它改成PNG格式的,结果就出现GDI+一般性错误,查了N久资料,才发现解决的办法,对分享此解决办法的网友深表感谢 Response.C ...