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.


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 Marcosfor contributing this image!

Example:

Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6

思路

Approach 1: Brute force

可以使用暴力破解的方法,即遍历所有的可能,从左往右遍历数组中的每个元素,找每个元素它左边最高的的高度max_left和它右边最高的高度max_right。计算这部分所能trap的水量,也就是max_left和max_right中的最小值减去这个元素的高度。换而言之,这种遍历的方法是遍历求每个横坐标单位上所能trap住的water,即 在这个单位横坐标上所能trap的water减去这个单位上竖条所占的面积:

代码:

class Solution {
public int trap(int[] height) {
int water=0; for(int i=0;i<height.length;i++){
int max_left=0;
int max_right=0;
for(int j=i;j>=0;j--){
max_left=Math.max(max_left, height[j]);
}
for(int k=i;k<height.length;k++){
max_right=Math.max(max_right, height[k]);
}
water=water+Math.min(max_right, max_left)-height[i];
} return water; }
}

Approach 2: Dynamic Programming

方法一种会重复计算左边的max_left和右边的max_right,可以考虑使用dp的方法将已计算的结果存起来避免重复计算的问题。基本想法就是维持两个数组 left_max[i] 和right_max[i],分别存放位置i左边的最大高度和右边的最大高度,然后在再遍历一边数组元素即可。

具体的计算是这样的,对于right_max[i],如果i的高度比right_max[i-1]要高,也就是说位置i的bar的高度比之前高,那么right_max[i]等于height[i],否则就是之前最高的高度,也就是right_left[i-1],计算right_max同理。

代码:

class Solution {
public int trap(int[] height) {
int ans=0;
if(height==null || height.length==0) return ans;
int size=height.length;
int[] left_max=new int[size];
int[] right_max=new int[size]; left_max[0]=height[0];
right_max[size-1]=height[size-1];
for(int i=1;i<size;i++){
left_max[i]=Math.max(left_max[i-1], height[i]);
}
for(int i=size-2;i>=0;i--){
right_max[i]=Math.max(right_max[i+1], height[i]);
} for(int i=0;i<size;i++){
ans+=Math.min(left_max[i], right_max[i])-height[i];
}
return ans;
}
}

Approach 3: Using stacks

方法三利用栈,遍历整个数组元素,如果当前bar的高度不大于栈顶的bar的高度(也就是不大于上一个bar),那么僵当前bar的索引加入到栈中。如果当前bar的高度大于栈顶bar的高度,可以的出一个结论是栈顶bar 是被当前 bar 和栈顶的前一个bar 困住的(bounded),所以我们能够弹出栈顶的bar并计算这个位置上所能trap的water。弹出栈顶计算完后,再判断当前current是否还是大于新的栈顶bar的高度,如果小于或等于则push入栈,否则继续按之前的执行。注意的是这种计算方法和解法一,解法二的本质不同在于,这个并不是按照单条bar来一个一个计算的,他是先判断两个bar构成的区域再乘以两个bar之间的距离这样来计算的,本质上是两种思路。

代码:

class Solution {
public int trap(int[] height) {
int ans=0;
if(height==null ) return ans;
Stack<Integer> st=new Stack(); int current=0;
while(current<height.length){
while(!st.isEmpty() && height[current]>height[st.peek()]){
int middle=st.peek();
st.pop();
if(st.isEmpty())
break;
int distance=current-st.peek()-1;// 算的是间隔距离,所以要减一
int bounded_height=Math.min(height[current], height[st.peek()])-height[middle];// 标记1
ans+=distance*bounded_height;
}
st.push(current++);
}
return ans;
}
}

标记1处开始看的时候不明白为什么要减去height[middle],其实想到(1)数组中有高度为0的bar也会被添加到栈中 (2)bar之间彼此临接。想到这两中情形的话就好理解多了。

还有Approach 4: Using 2 pointers方法,详细见:https://leetcode.com/problems/trapping-rain-water/solution/

LeetCode解题报告—— 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】#42. Trapping Rain Water

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

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

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

  5. leetcode problem 42 -- Trapping Rain Water

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

  6. LeetCode OJ 42. Trapping Rain Water

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

  7. 【LeetCode】042 Trapping Rain Water

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

  8. LeetCode: Trapping Rain Water 解题报告

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

  9. leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

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

随机推荐

  1. 学习操作Mac OS 之 常用命令

    ~ 符号在 Mac 甚至所有基于 Unix 和 Linux 的系统中都是代表当前用户的用户目录,.代表当前目录 配置环境变量语句:  source ~/.bash_profile 查看host文件语句 ...

  2. Linux之异步通知20160702

    异步通知,主要说的是使用信号的方式,同时使用信号也是实现进程之间通信的一种方式. 多的不说,我们直接看代码: 首先应用程序的: #include <sys/types.h> #includ ...

  3. snmp代码篇

    相关链接:Snmp学习笔记使用snmp4j实现Snmp功能(一)使用snmp4j实现Snmp功能(二)使用snmp4j实现Snmp功能(三) SNMP是英文“Simple Network Manage ...

  4. Coconuts HDU - 5925 二维离散化 自闭了

    TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams about eating. One day, ...

  5. 手脱ASProtect v1.23 RC1(有Stolen Code)

    1.载入PEID ASProtect v1.23 RC1 常见ASprotect版本壳: ASProtect 1.23 RC4 按shift+f9键26次后来到典型异常 ASProtect 1.31 ...

  6. shell编程:条件测试与比较(六)

    条件测试方法综述 test条件测试的简单语法及测试 范例6-1 测试文件(在test命令中使用-f选项:文件存在且为不同文件则表达式成立) [root@adminset ~]# test -f fil ...

  7. http中有关缓存相关的几个字段

    转载自:http://blog.csdn.net/lifeibo/article/details/5979572 Expires.Cache-Control.Last-Modified. ETag是R ...

  8. [Luogu 1168] 中位数

    中位数可以转化为区间第k大问题,当然是选择Treap实现名次树了啊.(笑) 功能十分简单的Treap即能满足需求--只需要插入与查找第大的功能. 插入第i个数时,如果i是奇数,随即询问当前排名第(i+ ...

  9. Windows下端口占用查看

    假如我们需要确定谁占用了我们的80端口 1.Windows平台在windows命令行窗口下执行:C:\>netstat -aon|findstr "80" TCP     1 ...

  10. 省队集训 Day7 选点游戏

    [题目大意] 维护一个$n$个点的图,$m$个操作,支持两个操作: 1. 连接$(u, v)$这条边: 2. 询问$u$所在的联通块中,能选出的最大合法的点数. 一个方案是合法的,当且仅当对于所有被选 ...