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!


【题目分析】

题目给出一个整数序列代表高度图,要求计算下雨后图中的积水量。


【思路】

我们只要找到高度图中的凹陷部分,然后把所有凹陷的容积加起来即可。那么如何找到这些凹陷部分呢?

有这样一个想法:给定高度图两端的高度,那些比这两端都低的部分肯定会形成凹陷,如果是相等则不形成凹陷,如果比当前高度高,则我们要重新确定高度图的两端高度。这个过程如下:

1. 初始两端最低高度high = 0, 容量capacity = 0;

2. height[begin] <= high,不形成凹陷,begin++;

3. height[begin] > high; height[end] > high;high = min(height[begin], height[end]); 因此high = 1;

此时height[begin] == high,height[end] == high;所以begin++,end--;

4. height[begin] < high;形成凹陷,capacity += high - height[begin];此时capacity  = 1;begin++;

5. height[begin] > high; height[end] > high;high = min(height[begin], height[end]); 因此high = 2;

此时height[begin] == high,height[end] == high;所以begin++,end--;

6. height[begin] < high;形成凹陷,capacity += high - height[begin];此时capacity  = 2;begin++;

height[end] < high;形成凹陷,capacity += high - height[begin];此时capacity  = 3;end--;

7. height[begin] < high;形成凹陷,capacity += high - height[begin];此时capacity  = 5;begin++;

height[end] = high;不形成凹陷;end--;

7. height[begin] < high;形成凹陷,capacity += high - height[begin];此时capacity  = 6;begin++;

8. 返回最大容量6;


【java代码】

 public class Solution {
public int trap(int[] height) {
if(height == null || height.length <= 2) return 0; int sum = 0, maxhigh = 0;
int begin = 0, end = height.length-1;
while(begin <= end){
if(height[begin] < maxhigh){
sum += maxhigh - height[begin++];
}
else if(height[end] < maxhigh){
sum += maxhigh - height[end--];
}
else{
maxhigh = Math.min(height[begin], height[end]);
if(height[begin] <= maxhigh) begin++;
if(height[end] <= maxhigh) end--;
}
}
return sum;
}
}

LeetCode OJ 42. Trapping Rain Water的更多相关文章

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

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

  2. 【LeetCode】42. Trapping Rain Water

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  3. leetcode problem 42 -- Trapping Rain Water

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

  4. 【一天一道LeetCode】#42. Trapping Rain Water

    一天一道LeetCode系列 (一)题目 Given n non-negative integers representing an elevation map where the width of ...

  5. 【LeetCode】42. Trapping Rain Water 接雨水 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...

  6. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

  7. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

  8. LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))

    LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...

  9. LeetCode - 42. Trapping Rain Water

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

随机推荐

  1. 项目中dubbo的使用

    导语:Dubbo是阿里巴巴的一个分布式服务的开源框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000 ...

  2. aforge通过角点匹配图片相似度

    我不知道什么原因,人品不好还是啥的 ExhaustiveTemplateMatching这个类无法高精确度的匹配图片 ........... 换一种方式,就好得多 /// <summary> ...

  3. sql时间转换函数--备忘

    总是忘记 一.语法: CAST (expression AS data_type) 参数说明: expression:任何有效的SQServer表达式. AS:用于分隔两个参数,在AS之前的是要处理的 ...

  4. noip 2016 提高组总结(不是题解)

    小弱鸡杨树辰是第一次参加像noip这样的高大上的比赛,于是他非常,非常,非常激动. 当他第二天考完试后,他正在yy自己的分数:day1T1应该是a掉了,T2写了个30分的暴力,T3也是个40分的暴力, ...

  5. 安装 zabbix 时遇到的一个问题

    安装 zabbix 2.0.3,到最后阶段,遇到一个问题: PHP bcmath extension missing,  php configuration parameter --enable-bc ...

  6. CodeForces 670B Game of Robots

    简单题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #inclu ...

  7. CodeForces 706B Interesting drink

    排序,二分. 将$x$数组从小到大排序,每次询问的时候只要二分一下位置就可以了. #pragma comment(linker, "/STACK:1024000000,1024000000& ...

  8. 从后台调用前台js

    引用: using System.Web.UI; ScriptManager.RegisterClientScriptBlock(this, GetType(), "Js", &q ...

  9. PAT乙级1006. 换个格式输出整数 (15)

    让我们用字母B来表示“百”.字母S表示“十”,用“12...n”来表示个位数字n(<10),换个格式来输出任一个不超过3位的正整数.例如234应该被输出为BBSSS1234,因为它有2个“百”. ...

  10. Git 的版本库创建和修改

    什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以追踪历史,或 ...