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. 卡特兰数(Catalan Number) 学习笔记

    一.三个简单的问题 1.给定一串长为2n的01序列,其中0和1的数量相等,满足任意前缀中0的个数不少于1的个数,求序列的个数 2.给出一串长为n的序列,按顺序将他们进栈,随意出栈,求最后进出栈的方案 ...

  2. 项目管理---git----快速使用git笔记(五)------本地项目代码提交到远程仓库---新建项目

    上一篇我们已经知道了怎么从远程仓库获取项目文件代码. 项目管理---git----快速使用git笔记(四)------远程项目代码的首次获取 git还有一种使用场景是 我本来在电脑里就有一个项目,现在 ...

  3. bzoj Usaco补完计划(优先级 Gold>Silver>资格赛)

    听说KPM初二暑假就补完了啊%%% 先刷Gold再刷Silver(因为目测没那么多时间刷Silver,方便以后TJ2333(雾 按AC数降序刷 ---------------------------- ...

  4. ubuntu16.04装chrome

    --更简单的方法是先下载chromium浏览器,这是不禁止的,然后打开chromium搜索chrome,chrome的官网下载即可   //安装好后,终端输入google-chrome即可打开 另一种 ...

  5. 剑指offer --合并链表

    题目:输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 解法://递归解法 public class MixLink { /* public class L ...

  6. intellij idea 中添加maven远程仓库

    在intellij idea 中配置好maven后 是这样的 如果加载失败,则需要自定义远程仓库,这里以阿里maven仓库为例, 在项目的pom文件中添加以下配置 <repositories&g ...

  7. Codeforces 671B/Round #352(div.2) D.Robin Hood 二分

    D. Robin Hood We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and ...

  8. 2015/9/1 Python基础(6):列表

    列表和字符串类型很相似,是同样的序列式数据类型.但是字符串只能由字符组成,列表可以保留任意数目的Python对象的灵活的容器.Python的列表比C的数组要灵活,数组里面只能是一种类型,列表可以有多种 ...

  9. Ubuntu12.04 安装LAMP及phpmyadmin

    1.安装 Apache apt-get install apache2 2.安装 PHP5 apt-get install php5 libapache2-mod-php5 3.安装 MySQL ap ...

  10. bzoj 1856 组合

    这道题有些类似卡特兰数的其中一种证明,总方案数是c(n+m,n),点(m,n)对应y=x-1对称点为(n+1,m-1),所以答案为c(n+m,n)-c(n+m,n+1). 反思:开始坐标轴画错了,结果 ...