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!

注意对每根柱子能盛的水并不是自身能决定的,而是取决于其左右两边的柱子。

先记录最高的柱子maxHeight,把数组一分为二,分别计算最高柱子左右两边的盛水量。

对于最高柱子左边的部分,

  从首端开始扫描,并使用leftHeight记录已扫描部分的中得最高柱子。

  如果leftHeight > curHeight,说明当前柱子能盛水,当前柱子所盛水的容量为leftHeight-curHeight

  如果leftHeight<= curHeight,说明当前柱子不能盛水,则更新leftHeight = curHeight

对于最高柱子右边的部分,从末端开始倒序扫描,求右半部分的水,即可

class Solution {
public:
int trap(int A[], int n) {
int maxHeightIndex = ;
for(int i = ; i < n; ++ i){
if(A[i] > A[maxHeightIndex]) maxHeightIndex = i;
}
int leftHeight = ,res = ;
for(int i = ; i < maxHeightIndex;++ i){
if(leftHeight > A[i]) res+=leftHeight-A[i];
else leftHeight = A[i];
}
int rightHeight = ;
for(int i = n-; i>maxHeightIndex; --i){
if(rightHeight > A[i]) res+=rightHeight-A[i];
else rightHeight = A[i];
}
return res;
}
};

Leetcode Trapping Rain Water的更多相关文章

  1. [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 ...

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

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

  3. LeetCode: Trapping Rain Water 解题报告

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

  4. 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 ...

  5. [leetcode]Trapping Rain Water @ Python

    原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers represe ...

  6. [LeetCode] Trapping Rain Water 栈

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

  7. [LeetCode] Trapping Rain Water II 题解

    题意 题目 思路 我一开始想的时候只考虑到一个结点周围的边界的情况,并没有考虑到边界的高度其实影响到所有的结点盛水的高度. 我们可以发现,中间是否能够盛水取决于边界是否足够高于里面的高度,所以这必然是 ...

  8. leetcode Trapping Rain Water pthon

    class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 ...

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

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

随机推荐

  1. linux 安装 ArcSDE10.1

    实验仍未成功,步骤仅供参考. 1:首先检查一下在Linux操作系统下Oracle数据库是否能启动,是否能连通等 [oracle@localhost ~]$ sqlplus SQL*Plus: Rele ...

  2. 2Struts2配置----青软S2SH(笔记)

    实例的文件目录如下:两个配置文件(web.xml和struts.xml),一个UserAction.java,三个jsp. 1.配置Struts2应用环境: 下载Struts2的jar包,官网是:ht ...

  3. Holt-Winters原理和初始值的确定

      关于模型 (来自以下PPT,从第4页开始)   关于初始值: 以下文档给出了三个模型的初始值计算的思路. 大致思路如下,建立一个p阶移动平均模型,估计出参数即为初始值,具体的根据三种不同的模型,有 ...

  4. oracle分区表知识

    在F5中查看执行计划的时候总是看到很多信息: range分区 执行计划中出现的: 分区表,按 n1 ,n2 分区 partition range single:访问单个分区 partition ran ...

  5. Alpha版本冲刺总结——曙光初现

    No Bug 031402401鲍亮 031402402曹鑫杰 031402403常松 031402412林淋 031402418汪培侨 031402426许秋鑫 项目预期计划 界面设计 androi ...

  6. Marquee 滚动参数

    new marquee("Layer3", 2, 1, 989, 68, 20, 0, 0, 238); 参数说明:Layer3 :容器ID 2 :向上滚动(0向上 1向下 2向左 ...

  7. jquery.SuperSlide.js只需要调用一个插件就能实现网页大部分特效--推荐

    很棒的一个插件,http://www.superslide2.com/

  8. Nginx的继续深入(日志轮询切割,重写,负载均衡等)

    Nginx的访问日志轮询切割 通常什么情况Nginx会把所有的访问日志生成到一个制定的访问日志文件access.log里面,但时间一长,日志个头很大不利于日志的分析和处理. 有必要对Nginx日志进行 ...

  9. 【krpano】二维码自动生成插件(源码+介绍+预览)

    简介 在krpano生成的全景支持HTML5在手机中展示,而在手机中打开全景网址时不方便,需要输入网址. 最近研究了如何让krpano全景根据自己当前的网址,自动生成二维码,并在电脑浏览时,可以展示出 ...

  10. selenium常用的js总结

    1. 对input执行输入 直接设置value属性, 此方法主要应对输入框自动补全以及readonly属性的element,sendkeys不稳定 比如: //inputbox is a WebEle ...