42. Trapping Rain Water (Array,stack; DP)
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!
思路:动态规划。第一次存储从开始到i最高的位置,求最高位置a之前的存水量=a与a之前最高位置b之间的水量+b与b之前最高位置c之间的水量...
第二次存储从末尾到i最高的位置,求最高位置a之后的存水量=a与a之前最高位置m之间的水量+m与m之前最高位置n之间的水量...
class Solution {
public:
int trap(vector<int>& height) {
int size = height.size();
if(size==) return ;
vector<int> dp(size,); //save the highest position until now
int ret = ;
int left,right;
//first traverse from left to right
for(int i = ; i < size; i++){
//state transfer
if(height[i] > height[dp[i-]]) dp[i]=i;
else dp[i] = dp[i-];
}
//calculate the water to the left of the highest position
left = dp[size-];
while(left>){
right=left;
left=dp[right-];
for(int i = left+; i < right; i++){
ret += (height[left]-height[i]);
}
}
//second traverse from right to highest pos
int highestPos=dp[size-];
dp[size-]=size-;
for(int i = size-; i >= highestPos; i--){
//state transfer
if(height[i] > height[dp[i+]]) dp[i]=i;
else dp[i] = dp[i+];
}
//calculate the water to the right of the highest position
right=highestPos;
while(right<size-){
left=right;
right=dp[left+];
for(int i = left+; i < right; i++){
ret += (height[right]-height[i]);
}
}
return ret;
}
};
改进:用stack代替vector作为状态存储。stack的栈顶是到目前为止最大元素的下标,因为最高位置是关键,找到最高位置,可以往左,往右计算水位。
stack的实现类似用两个stack实现能够返回最大元素的stack。
class Solution {
public:
int trap(vector<int>& height) {
int size = height.size();
if(size==) return ;
stack<int> s;
s.push();
int i, ret = , highestPos;
//First traverse from left to right
for(int i = ; i < size; i++){
if(height[i]<=height[s.top()]) continue;
s.push(i);
}
i=s.top();
highestPos = i;
while(){
if(i==s.top()){
s.pop();
if(s.empty()) break;
}
else{
ret+=(height[s.top()]-height[i]);
}
i--;
}
//then traverse from right to left
s.push(size-);
for(int i = size-; i >= highestPos; i--){
if(height[i]<=height[s.top()]) continue;
s.push(i);
}
i=highestPos;
while(){
if(i==s.top()){
s.pop();
if(s.empty()) break;
}
else{
ret+=(height[s.top()]-height[i]);
}
i++;
}
return ret;
}
};
42. Trapping Rain Water (Array,stack; DP)的更多相关文章
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- 刷题42. Trapping Rain Water
一.题目说明 题目是42. Trapping Rain Water,翻译起来就是"接雨水".给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水.题目难度是Hard 二.我的 ...
- [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(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
随机推荐
- [转]批处理遍历文件夹生成 html 文件
[转自] http://www.360doc.com/content/15/0205/20/21861372_446525665.shtml :: 自动将指定文件夹中的图片写入到 html 文件中 @ ...
- supervisord管理进程详解
supervisord管理进程详解 supervisor配置详解(转) 官网 Linux后台进程管理利器:supervisor supervisor使用详解
- html如何设置打印样式?
转自网络,忘记出处了. html/jsp/网页/打印相关/打印预览/js设置页眉页脚 <html> <head> <title>打印相关</title& ...
- Leetcode 之Simplify Path @ python
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- nginx的日志分析
1.到NGINX把日志DOWN下来2.用命令cat xxxx.log | egrep '10/Jul/2015:01:[4-5]|2015-07-10 02:0[0-57]'>xxxx2.log ...
- javascript继承之借用构造函数(二)
//简单的函数调用 function Father() { this.nums= [1,2]; } function Son() { Father.call(this);//调用超类型,完成son继承 ...
- jQuery动态的给页面中添加一条样式表的链接
HTML部分: <input type="button" value="单击" onclick="getbody()" /> & ...
- ubuntu18.04修改时区
运行如下命令: sudo tzselect 然后选择亚洲Asia,继续选择中国China,最后选择北京Beijing. 然后创建时区软链 sudo ln -sf /usr/share/zoneinfo ...
- spark 多语言编程
参考官方地址:https://spark.apache.org/docs/1.6.2/programming-guide.html 误解: spark多语言的支持,并不是说spark可以操作各个语言写 ...
- Win7关机时弹出对话框,提示你想要的信息
博主换了个公司,要求每天写日志,次日8点前没写的话就要扣钱,1篇10块钱,博主已经两次写完忘记提交到ERP系统了,捂脸... 因为公司要求所有工作在公司配的台式电脑上,所以如果能在关机前弹个提示(不关 ...