leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法
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(凹槽的雨水) 解题思路和方法的更多相关文章
- [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 ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [leetcode]42. Trapping Rain Water雨水积水问题
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] 42. Trapping Rain Water 解题思路
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- Java [Leetcode 42]Trapping Rain Water
题目描述: Given n non-negative integers representing an elevation map where the width of each bar is 1, ...
随机推荐
- 类命名空间与对象、实例的命名空间 and 面向对象的组合用法
类命名空间与对象.实例的命名空间 创建一个类就会创建一个类的名称空间,用来存储类中定义的所有名字,这些名字称为类的属性 而类有两种属性:静态属性和动态属性 静态属性就是直接在类中定义的变量 动态属性就 ...
- 安卓 自定义ViewGroup
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 指定的 布局参数 在计算时候方法 中 计算 所有 子视图的 宽高, 然后根据这些 计算出 ...
- codevs 1102 采药 2005年NOIP全国联赛普及组
1102 采药 2005年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB gold 题目描述 Description 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最 ...
- 【二项式定理】【DFS】UVALive - 7639 - Extreme XOR Sum
题意:一个序列,q次询问,每次问你某个指定区间内的EXtreme XOR值. 一个长度为l的区间的EXtreme XOR值被定义为,从左到右,将每相邻的两个数XOR起来,产生l-1个新的值,……如此循 ...
- 2017-2018-1 JAVA实验站 冲刺 day05
2017-2018-1 JAVA实验站 冲刺 day05 各个成员今日完成的任务 小组成员 今日工作 完成进度 张韵琪 进行工作总结 100% 齐力锋 找按钮音乐 100% 张浩林 写博客 100% ...
- 1.6(SQL学习笔记)存储过程
一.什么事存储过程 可以将存储过程看做是一组完成某个特定功能的SQL语句的集合. 例如有一个转账功能(A向B转账50),先将账户A中金额扣除50,然后将账户B中金额添加50. 那么我们可以定义一个名为 ...
- 中国剩余定理 hdu 3579
HDU 3579 Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 我弄的一些TASKER配置
http://tieba.baidu.com/p/2184969007 我弄的一些配置,需要的童鞋们找自己想要的吧,有些配置感觉还是很繁琐,请高手不吝赐教才好,图片太多,就发链接了. ◆保持屏幕开启, ...
- FreeRTOS Customisation -- FreeRTOSConfig.h
http://www.freertos.org/a00110.html FreeRTOS is customised using a configuration file called FreeRTO ...
- .Net4.0并行库介绍——Cancellation Framework
在.net 4.0中,引入了一个新的类CancellationToken,这个类基本上集成了我们各种常用的取消方式,在并发任务中非常有用. 同步模式下的取消: 一种比较常见的需要支持取消功能的的是一些 ...