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. java实验五——字符数组、String、StringBuffer的相互转化,StringBuffer的一些方法

    package hello; import java.util.Scanner; public class 实验五 { public static void main(String[] args) { ...

  2. MongoDB CPU 利用率高,分析慢请求

    Jemeter 压测过程,发现mongodb的CPU均达到100%,需要查看mongodb的执行情况,使用mongo自带的profiling功能. profiling将请求的执行情况记录在DB下的 s ...

  3. Linux故障-bash-4.1$

    #模拟故障:-bash-4.1$ [root@nodchen ~]# su - cisco[cisco@nodchen ~]$ \rm -f .*rm: cannot remove `.': Is a ...

  4. 1062 Talent and Virtue (25 分)

    1062 Talent and Virtue (25 分) About 900 years ago, a Chinese philosopher Sima Guang wrote a history ...

  5. [UE4]IES光源概述文件

    IES Light Profiles(IES光源概述文件) 是一条曲线,该曲线在一段弧线中定义了光源强度,虚幻引擎4将会围绕某个轴旋转该弧线,从而使得 点光源 (和从技术上讲的 聚光源,下面会提供更多 ...

  6. C++多线程同步之事件(Event)

    原文链接:http://blog.csdn.net/olansefengye1/article/details/53291074 一.事件(Event)原理解析 1.线程同步Event,主要用于线程间 ...

  7. php 学习笔记 设计和管理

    代码管理 文件路径.数据库名.密码禁止 hard coded 避免重复代码在多个页面复制粘贴 Gang of Four eXtreme Programming 的主要原则是坚决主张测试是项目成功的关键 ...

  8. Python分页转Mybatis pagehelper格式分页

    最近工作里遇到一个需求要把之前用Java写的一个http接口替换成用Python写的,出参是带了mybatis pageHelper中PageInfo信息的一个JSON串,而Python这边分页不会涉 ...

  9. linux系统更新rpm包问题 ,报错rhn-check-2.0.2-5.el7.noarch has missing requires of yum-rhn-plugin >= ('0', '1.6.4', '1')

    报错信息: rhn-check-2.0.2-5.el7.noarch has missing requires of yum-rhn-plugin >= ('0', '1.6.4', '1') ...

  10. WINdows常用监控相关

    参考网址: http://www.jb51.net/article/49986.htm 一.图新Shell下: 1.    最直观的:(在运行里面输入CMD,以下命令都是在CMD下输入的:) 输入 s ...