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. 洛谷 P4175 [CTSC2008]网络管理 解题报告

    P4175 [CTSC2008]网络管理 题目描述 带修改树上链的第\(k\)大 输入输出格式 输入格式: 第一行为两个整数\(N\)和\(Q\),分别表示路由器总数和询问的总数. 第二行有\(N\) ...

  2. ACE线程管理机制-并发控制(3)

    转载于:http://www.cnblogs.com/TianFang/archive/2006/12/04/581854.html ACE Condition类属 ACE Condition类属(条 ...

  3. AtCoder Grand Contest 031 B - Reversi(DP)

    B - Reversi 题目链接:https://atcoder.jp/contests/agc031/tasks/agc031_b 题意: 给出n个数,然后现在你可以对一段区间修改成相同的值,前提是 ...

  4. Android数据库资料

    一.联系人和通话记录: 数据库文件/data/data/com.android.providers.contacts/databases/contacts2.db  通话记录的数据存在calls表中; ...

  5. Bigbluebutton服务执行过程及相关配置文件

    BigBlueButton服务列表 BigBlueButton由许多开源的服务组成,看似很麻烦,实际上拆分开每一个服务就很简单了,组件化平台化.究竟BBB都用到了哪些开源服务?我们来列举一下,名称均带 ...

  6. div模拟textarea在ios下不兼容的问题解决

    今天发现一个好东西,赶紧记下来,我在用textarea的时候,想要自适应高度,这样就不会出现滚动条.网上找了很多,都是用div模拟的,但是好扯淡,div模拟的在ios下不能聚焦并且不能输入.真坑... ...

  7. 转:为什么在定义hashcode时要使用31这个数呢?

    散列计算就是计算元素应该放在数组的哪个元素里.准确的说是放到哪个链表里面.按照Java的规则,如果你要想将一个对象放入HashMap中,你的对象的类必须提供hashcode方法,返回一个整数值.比如S ...

  8. 单词转换成向量形式 word2vec

    word2vec(word to vector)是一个将单词转换成向量形式的工具.可以把对文本内容的处理简化为向量空间中的向量运算,计算出向量空间上的相似度,来表示文本语义上的相 似度.word2ve ...

  9. centos6.8+openvpn实现账户密码连接(通过端口映射的方式)

    #搭建openvpn(编译安装) 初始化环境 #update epel mirror yum install wget -y cd /etc/yum.repos.d && rm -rf ...

  10. .NET中使用switch和java不一样的地方。

    1.不能这样贯穿 我们知道,java 和 C在使用switch时候可以这样. switch (i) { //java中此处不使用break // 执行了case 1:对应的语句后直接 贯穿到 case ...