接雨水

给出 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. linux系统下sd卡的备份与恢复

    linux系统下sd卡的备份与恢复 现在各种的开发板都是从sd卡上面启动的,因此大修改工作之前很有必要备份一下. 备份 在linux系统下用读卡器读取sd卡 用df -h命令看分区的路径 一般都是/d ...

  2. iOS9新系统下APP Store 应用上传新指南

    一 iTunes Connect介绍 iTunes Connect是面向iOS应用开发人员的苹果门户网站,供开发人员管理其应用,跟踪下载情况.今年1月份闹得沸沸扬扬的iTunes Connect BU ...

  3. 微软职位内部推荐-Software Engineer II-SDP

    微软近期Open的职位: Position: SDE II The R&D of Shared Data Platform at Application and Services Group ...

  4. android 开发 解码gif图片,获取每帧bitmap

    环境:android 4.3  (注意对于android4.4版本解码出来不正确,除了第一帧正确外,其余的都是显示不同的地方)  通用版本见: android 开发对gif解码(适配android 4 ...

  5. 【Unique Binary Search Trees】cpp

    题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...

  6. Netsharp快速入门(之8) 基础档案(工作区2 设置商品主列表、规格细列表、商品表单、查询)

    作者:秋时 杨昶   时间:2014-02-15  转载须说明出处 3.5.1.1 列表设置 1.选择第一行主列表,点工具-列表方案 2.打开列表方案界面后,在列表项目填入需要用到实体Demo.Arc ...

  7. 看我是一只IT小小鸟有感

    当我看了<我是一只IT小小鸟>后,有许多的感想.就像许多作者一样,在接触计算机这个专业时都有许多的抱怨,对这个专业的不了解,对这个专业不知道从何学起有深深的无助感,对这个专业在未来的发展有 ...

  8. navigationController.navigationBar.titleTextAttributes

    navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor ...

  9. javascript 字符串和json的互转

    FROM:http://www.cnblogs.com/mizzle/archive/2012/02/10/2345891.html 在数据传输过程中,json是以文本,即字符串的形式传递的,而JS操 ...

  10. Swift-2-基本操作符

    // Playground - noun: a place where people can play import UIKit // 基本运算符 // 运算符有3种: 单目运算符(如 -a),二目运 ...