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之间的水量...

  1. class Solution {
  2. public:
  3. int trap(vector<int>& height) {
  4. int size = height.size();
  5. if(size==) return ;
  6.  
  7. vector<int> dp(size,); //save the highest position until now
  8. int ret = ;
  9. int left,right;
  10.  
  11. //first traverse from left to right
  12. for(int i = ; i < size; i++){
  13. //state transfer
  14. if(height[i] > height[dp[i-]]) dp[i]=i;
  15. else dp[i] = dp[i-];
  16. }
  17.  
  18. //calculate the water to the left of the highest position
  19. left = dp[size-];
  20. while(left>){
  21. right=left;
  22. left=dp[right-];
  23. for(int i = left+; i < right; i++){
  24. ret += (height[left]-height[i]);
  25. }
  26. }
  27.  
  28. //second traverse from right to highest pos
  29. int highestPos=dp[size-];
  30. dp[size-]=size-;
  31. for(int i = size-; i >= highestPos; i--){
  32. //state transfer
  33. if(height[i] > height[dp[i+]]) dp[i]=i;
  34. else dp[i] = dp[i+];
  35. }
  36.  
  37. //calculate the water to the right of the highest position
  38. right=highestPos;
  39. while(right<size-){
  40. left=right;
  41. right=dp[left+];
  42. for(int i = left+; i < right; i++){
  43. ret += (height[right]-height[i]);
  44. }
  45. }
  46.  
  47. return ret;
  48. }
  49. };

改进:用stack代替vector作为状态存储。stack的栈顶是到目前为止最大元素的下标,因为最高位置是关键,找到最高位置,可以往左,往右计算水位。

stack的实现类似用两个stack实现能够返回最大元素的stack。

  1. class Solution {
  2. public:
  3. int trap(vector<int>& height) {
  4. int size = height.size();
  5. if(size==) return ;
  6.  
  7. stack<int> s;
  8. s.push();
  9. int i, ret = , highestPos;
  10.  
  11. //First traverse from left to right
  12. for(int i = ; i < size; i++){
  13. if(height[i]<=height[s.top()]) continue;
  14.  
  15. s.push(i);
  16. }
  17.  
  18. i=s.top();
  19. highestPos = i;
  20. while(){
  21. if(i==s.top()){
  22. s.pop();
  23. if(s.empty()) break;
  24. }
  25. else{
  26. ret+=(height[s.top()]-height[i]);
  27. }
  28. i--;
  29. }
  30.  
  31. //then traverse from right to left
  32. s.push(size-);
  33. for(int i = size-; i >= highestPos; i--){
  34. if(height[i]<=height[s.top()]) continue;
  35.  
  36. s.push(i);
  37. }
  38.  
  39. i=highestPos;
  40. while(){
  41. if(i==s.top()){
  42. s.pop();
  43. if(s.empty()) break;
  44. }
  45. else{
  46. ret+=(height[s.top()]-height[i]);
  47. }
  48. i++;
  49. }
  50. return ret;
  51. }
  52. };

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. poj 3069 Saruman's Army(贪心)

    Saruman's Army Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tot ...

  2. 学习笔记之Moq

    dotnet/src/MoqSample at master · haotang923/dotnet · GitHub https://github.com/htanghtang/dotnet/tre ...

  3. shell_sctipts: 删除mysql备份到最后7日

    目前,mysql的备份文件,经过一周左右清理,手动清理会比较费事,所以写了一个简单脚本来实现. 前提介绍: mysql备份文件放在/usr/bak/sql里面,sql文件的备份名称格式为: mysql ...

  4. 新生代老年代GC组合

    新生代通常存活时间较短,因此基于Copying算法来进行回收,所谓Copying算法就是扫描出存活的对象,并复制到一块新的完全未使用的空间中 在执行机制上JVM提供了串行GC(SerialGC).并行 ...

  5. [UE4]动画序列面板

  6. 不重启修改'log_slave_updates'变量

    Variable 'log_slave_updates' is a read only variable 不重启修改mysql变量 执行复制的时候遇到的问题 mysql> show variab ...

  7. Impala源码分析

    问题导读:1.Scheduler任务中Distributed Plan.Scan Range是什么?2.Scheduler基本接口有哪些?3.QuerySchedule这个类如何理解?4.Simple ...

  8. Solr优化案例分析

    随着umc接入主机的数量越来越多,每天产生的syslog日志数量也在剧增, 之前一天产生的syslog数量才不 到1W,随着整个集团的网络设备不端接入,导致现在每天产生的syslog数量大概在180w ...

  9. python中函数的返回值

    函数返回值(一) <1>“返回值”介绍 现实生活中的场景: 我给儿子10块钱,让他给我买包烟.这个例子中,10块钱是我给儿子的,就相当于调用函数时传递到参数,让儿子买烟这个事情最终的目标是 ...

  10. python 编写远程连接服务器脚本

    import paramiko client = paramiko.SSHClient()client.set_missing_host_key_policy(paramiko.AutoAddPoli ...