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. Jmeter 问题集

    1.配置分布式,调度机(master) 看不到 执行机(slave). 原因: slave是放在一个交换机下面,然后在这个交换机下面又接了个路由器,control连的这个路由器 解决: CONTROL ...

  2. Spring IOC - 控制反转(依赖注入) - 懒加载机制

    懒加载机制 Spring默认会在容器初始化的过程中,解析xml,并将单例的bean创建并保存到map中,这样的机制在bean比较少的时间问题不大,但一旦bean非常多时,Spring需要在启动的过程中 ...

  3. ASP.NET Web Pages:目录

    ylbtech-.Net-ASP.NET Web Pages:目录 1. 官网返回顶部 1. https://www.asp.net/web-pages 2. https://msdn.microso ...

  4. java 为什么wait(),notify(),notifyAll()必须在同步方法/代码块中调用?

    在Java中,所有对象都能够被作为"监视器monitor"——指一个拥有一个独占锁,一个入口队列和一个等待队列的实体entity.所有对象的非同步方法都能够在任意时刻被任意线程调用 ...

  5. JavaScript中的坑

    内容:关于JavaScript中的一些蛋疼的问题以及面试笔试中常见的一些坑爹套路总结 此部分内容持续总结完善中... 1.undefined和null的区别 null: Null类型,代表空值,代表一 ...

  6. jsp 传多个值给后端

     页面上是这样 http://localhost:8080/smartcloset/getClothByCategory/1/11 直接用/分 后台是这样取的 @RequestMapping(valu ...

  7. leetcode179

    class Solution { public: string largestNumber(vector<int>& nums) { int n=nums.size(); vect ...

  8. leetcode202

    public class Solution { private int SumSqares(int n) { //将一个数字的各个数位的值分开存储 var list = new List<int ...

  9. jackson的小知识

  10. python global nonlocal

    global: 方法之外在modual中的变量定义为全局变量.方法内的变量为局部变量. 一般情况下,全局变量可以被使用,但是不应该被修改,不然会报错. 不过一般不建议对全局变量做修改,如果有多个方法都 ...