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. Cookie&Session会话技术

    一.会话技术简介 1.存储客户端的状态 由一个问题引出今天的内容,例如网站的购物系统,用户将购买的商品信息存储到哪里?因为Http协议是无状态的,也就是说每个客户访问服务器端资源时,服务器并不知道该客 ...

  2. 解决XP系统访问Win10打印机被拒绝的问题

    打印机是办公室人员经常会用到的设备,为了方便多人使用都会将打印机设置共享,可是会有许多xp系统用户需要访问win10系统上的打印机,这时候却发现拒绝访问无法连接,该如何解决呢? 其实这是win10做的 ...

  3. Java高级架构师(一)第43节:Varnish简介、安装和基本使用

    第一部分:Varnish简介 Varnish是一款开源的.高性能的HTTP加速器和反向代理服务器. Varnish反向代理的能力远不如Nginx. Varnish主要作用是HTTP的加速器,主要通过缓 ...

  4. mybatis源码分析(5)-----拦截器的实现原理(动态代理+责任链)

    写在前面 MyBatsi 的拦截器模式是基于代理的代理模式.并且myBatis 的插件开发也是以拦截器的形式集成到myBatis 当中. MyBatis 的拦截器已经插件是在org.apache.ib ...

  5. react-native-image-zoom-viewer学习

    github原地址 react-native-image-zoom-viewer实现了类似微信朋友圈浏览图片的效果,点击小图片实现浏览原图效果. 安装: npm i react-native-imag ...

  6. Visual Studio 2015创建Shared Project时出错

    今天使用Visual Studio 2015创建共享项目的时候发现如下错误: 网上搜了一下,发现了同样有人问这个问题的问题:Why can't I create Shared Project in V ...

  7. Dual transistor improves current-sense circuit

    In multiple-output power supplies in which a single supply powers circuitry of vastly different curr ...

  8. PWM DAC Low Pass Filtering

    [TI博客大赛][原创]LM3S811之基于PWM的DAC http://bbs.ednchina.com/BLOG_ARTICLE_3005301.HTM http://www.fpga4fun.c ...

  9. struts2类型转化器详解(带例子)

    Struts2有两种类型转化器: 一种局部,一种全局. 如何实现: 第一步:定义转化器 第二部:注册转化器 下面做一个局部类型转化器的实例. 我们在上面一片日志说过有个变量date类型的.只有我们输入 ...

  10. 虚拟机安装Linux过程和踩坑

    由于想学习node,服务器端大都使用Linux系统,所以就想着在笔记本上弄个虚拟机,装上Linux,使用xshell在window上操作也方便,也借此来熟悉一下Linux,接下来就解释下安装的步骤和遇 ...