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!

思路:此题咋一看简单,可是细细思考。越想越复杂,感觉无从下手,无奈想了一天没搞定,仅仅能求助网上资料。最终思路例如以下:(网友非常强大)

(參考网址:http://www.xuebuyuan.com/1586534.html)

最后黑体字的Thanks Marcos的意思是让我们放大我们的想象力。由于上图非常easy误导我们的。假设仅仅有上图的情况的话,我们能够非常好用贪心法解决,可是。用贪心法是会出错的,由于假设我们计算当前最低点,然后往两边扩展找到两边递增的最高点,那么这个最高点非常可能是局部最高点,答案就会出错。

有两个解题思路:

1 两边往中间搜索

2 由左往右搜索,跳跃式计算

如以下的分析图,会比較直观:

蓝色代表水,能够看见非常多局部最高点都被水淹了。

这里计算面积不用一般几何书的方法,这里是两边往中间遍历,记录当前第二高点secHight,然后利用这个第二高点减去当前历经的柱子。剩下就装水容量了。

为什么是第二高点?由于两边比較。最高的点不用动,仅仅移动第二高点。

第一个思路,依照上图的思想就能写出很简洁的程序了,时间复杂度为O(n):

本人照着上面的思路写的代码例如以下:

public class Solution {
public int trap(int[] height) {
Stack<Integer> st = new Stack<Integer>();
if(height.length == 0)
return 0;
int i = 0;
int j = height.length - 1;
int ans = 0;//返回的答案
int secHight = 0;//第二个高度(最高的那个不动)
while(i < j){
if(height[i] < height[j]){
secHight = Math.max(secHight,height[i]);
//由于长度为1,高度也就是面积值。假设height[i]==secHight,则新增面积为0
ans += secHight - height[i];
i++;
}else{
secHight = Math.max(secHight,height[j]);
//由于长度为1,高度也就是面积值。假设height[i]==secHight,则新增面积为0
ans += secHight - height[j];
j--;
}
}
return ans;
}
}

leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法的更多相关文章

  1. [LeetCode] 407. Trapping Rain Water II 收集雨水 II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

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

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

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

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

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

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

  5. LeetCode - 42. Trapping Rain Water

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

  6. [LeetCode] 42. Trapping Rain Water 收集雨水

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

  7. [leetcode]42. Trapping Rain Water雨水积水问题

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

  8. [LeetCode] 42. Trapping Rain Water 解题思路

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

  9. Java [Leetcode 42]Trapping Rain Water

    题目描述: Given n non-negative integers representing an elevation map where the width of each bar is 1, ...

随机推荐

  1. linux——(3)文件与目录管理

    文件与目录管理相关指令 ls [-adlR] 目录 #查看目录与文件的命令. -a #连同隐藏文件一起列出来. -d #只列出目录. -l #列出相关属性和权限等数据. -R #连同子目录内容一起列出 ...

  2. 变量的解构赋值--ES6

    1. 数组的解构赋值 基本用法 let [a, b, c] = [1, 2, 3]; let [a,,c] = [1,2,3]; let [a,...b] = [1,2,3]; // a=1; b=[ ...

  3. ubuntu下mysql的安装

    1.在终端输入 sudo apt-get install mysql-server mysql-client 2.在此安装过程中会让你输入root用户(管理MySQL数据库用户,非Linux系统用户) ...

  4. Codeforces 1129 E.Legendary Tree

    Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1​\) 次 \((S=\{1\},T=\{ ...

  5. [NOI2015]寿司晚宴 --- 状压DP

    [NOI2015]寿司晚宴 题目描述 为了庆祝NOI的成功开幕,主办方为大家准备了一场寿司晚宴. 小G和小W作为参加NOI的选手,也被邀请参加了寿司晚宴. 在晚宴上,主办方为大家提供了n−1种不同的寿 ...

  6. BZOJ 3676: [Apio2014]回文串 后缀自动机 Manacher 倍增

    http://www.lydsy.com/JudgeOnline/problem.php?id=3676 过程很艰难了,第一次提交Manacher忘了更新p数组,超时,第二次是倍增的第0维直接在自动机 ...

  7. [转]Android学习:EditText的使用方法

        EditText是在Android开发中经常被使用到的控件,主要用来获取用户的输入内容.   1.EditText常用属性   EditText继承自TextView,所以EditText也拥 ...

  8. bzoj 4001 [TJOI2015]概率论 数学

    4010: [HNOI2015]菜肴制作 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/ ...

  9. 在阅读sqlmap源码时学到的知识--检查运行环境

    最近在读sqlmap的源码,懵懵懂懂中页大约学到了一些知识(说给自己听的话:由此可见,所谓的能够解决所有遇到问题的python水平,只能说明你遇见的都是简单的需求....),老规矩,在这里写一下,一则 ...

  10. <摘录>CentOS6.5下添加epel源

    0.安装yum优先级插件 yum install yum-priorities 1.epel简介: https://fedoraproject.org/wiki/EPEL/zh-cn rpm -Uvh ...