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)的更多相关文章

  1. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

  2. 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 用双指针向中间滑动,较小的高度就作为当前情 ...

  3. LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))

    LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...

  4. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

  5. LeetCode - 42. Trapping Rain Water

    42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...

  6. [Leetcode][Python]42: Trapping Rain Water

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...

  7. 刷题42. Trapping Rain Water

    一.题目说明 题目是42. Trapping Rain Water,翻译起来就是"接雨水".给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水.题目难度是Hard 二.我的 ...

  8. [LeetCode] 42. Trapping Rain Water 收集雨水

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

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

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

随机推荐

  1. 求交错序列前N项和(15 分)

    7-2 求交错序列前N项和(15 分) 本题要求编写程序,计算交错序列 1-2/3+3/5-4/7+5/9-6/11+... 的前N项之和. 输入格式: 输入在一行中给出一个正整数N. 输出格式: 在 ...

  2. 【转载】html中object标签详解

    [转载自http://blog.csdn.net/soliy/archive/2010/03/22/5404183.aspx] html标签之Object标签详解 作者:网络    出处:网络     ...

  3. Access-Control-Allow-Origin 跨域问题

    1.同源.同源策略(Same origin policy) 同源指的是协议,端口,域名全部相同. 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺 ...

  4. Web 单元测试

    问题描述: The import org.junit.Test conflicts with a type defined in the same file 导入的org.junit.Test和一个相 ...

  5. 初步认识AutoMapper

      AutoMapper 初步认识AutoMapper 前言 手动映射 使用AutoMapper 创建映射 Conventions 映射到一个已存在的实例对象   前言 通常在一个应用程序中,我们开发 ...

  6. 可怕的SCN!(转载)

    一.什么是SCNSCN(System Change Number 简称 SCN)是当Oracle数据库更新后,由DBMS自动维护去累积递增的一个数字.在Oracle中,有四种SCN,分别为:系统检查点 ...

  7. QQ去除聊天框广告详解——2016.9 版

    QQ聊天框广告很烦人,百度出来的一些方法早已过时,下面是博主整理出来的方法,供各位同学参考. 1.按键盘上的 Win+R 快捷键打开运行框,然后复制并粘贴 Application Data\Tence ...

  8. openstack 基镜像与差异镜像的整合

  9. HTML5 Canvas ( 事件交互, 点击事件为例 ) isPointInPath

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. springboot web项目的单元测试

    不废话,直接上代码. //// SpringJUnit支持,由此引入Spring-Test框架支持! @RunWith(SpringJUnit4ClassRunner.class) //// 指定我们 ...