LeetCode解题报告—— 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.
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的更多相关文章
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- 【LeetCode】42. Trapping Rain Water
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- 【一天一道LeetCode】#42. Trapping Rain Water
一天一道LeetCode系列 (一)题目 Given n non-negative integers representing an elevation map where the width of ...
- 【LeetCode】42. Trapping Rain Water 接雨水 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...
- leetcode problem 42 -- Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode OJ 42. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 【LeetCode】042 Trapping Rain Water
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- LeetCode: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...
- leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
随机推荐
- HDOJ.1789 Doing Homework again (贪心)
Doing Homework again 点我挑战题目 题意分析 给出n组数据,每组数据中有每份作业的deadline和score,如果不能按期完成,则要扣相应score,求每组数据最少扣除的scor ...
- poj2409:Let it Bead(置换群 polya定理)
题目大意:长度为n的项链,要染m种颜色,可以通过旋转或翻转到达的状态视为同一种,问有多少种染色方案. 学了一波polya定理,发现很好理解啊,其实就是burnside定理的扩展. burnside定理 ...
- Mysql数据库的主从复制
怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下: 1.主从服务器分别作以下操作: 1.1.版本一致 1.2.初始化表,并在后台启动mysql 1.3.修改root的密码 2.修 ...
- 学习tcpIp必备的抓包工具wireshark
wireshark是一个优秀的抓包工具 ip.src=192.168.10.123 发送http的一端 ip.dst=192.168.10.126 接收http的一端 如下图所示:
- [ 转载]Tomcat7 catalina.out 日志分割
http://m.blog.csdn.net/blog/mark_qi/8864644 最近由于工作需要,tomcat 的catalina.out文件的不断扩大,导致系统磁盘空间边变小,而且管理也难于 ...
- TCP ------ TCP三次握手(建立连接)及其相关内容
1.TCP客户端要连接到TCP服务器,需要经过三个过程: 以下是通过 Wireshark 抓取的三次握手数据包 → [SYN] Seq= Win= Len= MSS= → [SYN, ACK] Seq ...
- 不要在linux上启用net.ipv4.tcp_tw_recycle参数
不要在linux上启用net.ipv4.tcp_tw_recycle参数 发布于 2015/07/27 莿鸟栖草堂 本文为翻译英文BLOG<Coping with the TCP TIME-WA ...
- php 获取周几
date("l"); //date就可以获取英文的星期比如Sunday date("w"); //这个可以获取数字星期比如123,注意0是星期日 获取中文星期几 ...
- scrapy架构设计分析
scrapy是一个Python爬虫框架.我们自己用requests也能写爬虫(GET某个URL,然后Parse网页的内容),那么,问题来了,scrapy高明在哪些地方呢?下面就来讨论下这个话题,看看业 ...
- emqtt新版升级一些事项和操作
注解 Erlang/OTP R19依赖lksctp-tools库 yum install lksctp-tools 控制台地址: http://127.0.0.1:18083,默认用户: admin, ...