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!

Hide Tags

Array Stack Two Pointers

 

     好开心,逻辑这么麻烦的题目,写了一次没有错,提交了直接过。
     做了前面题目可能容易想,假如用一个stack 维护下标,其对应的数列 是非升序的,后面的工作中就维护这一个stack。在一个非降序的stack,如果考虑的值比top 小,则加入stack。如果大于top 值,则弹出小于的值,计算return 值。
     例如遍历到idx =5时候,stack 里面的 下标有:  3,4
  a[5]<a[4],所以加入stack,然后遍历到idx =6时候,stack为:3,4,5
  那么弹出 stack 的top,然后计算弹出值水坑,这时候只需要计算红色的部分,通过stack 弹出后的top=4,坑=5,和墙=6,的时候更新,
  然后更新stack 变成:3,4,6,接着便是遍历idx =7,这时候弹出的是6,通过top=4,坑6,墙=7 计算,这个值为0.
  因为stack 的top 还是小于,所以继续弹出,然后计算,top=3,坑4,墙7,计算了下图的部分:
 
 
 此时stack: 3,继续弹出,但此时stack 没有top 了,所以可以结束这一步,将idx=7 压入stack。
 
代码:
 
 #include <iostream>
#include <stack>
using namespace std; class Solution {
public:
int trap(int A[], int n) {
if(n<) return ;
int curIdx = ; stack<int> stk; int retSum = ;
for(;curIdx<n;curIdx++){
if(stk.empty()){
stk.push(curIdx);
continue;
}
int stkTop = stk.top();
if(A[stkTop]>=A[curIdx]){
stk.push(curIdx);
continue;
}
while(!stk.empty()){
int dit = stkTop;
stk.pop();
if(stk.empty()) break;
stkTop =stk.top();
retSum += (min(A[stkTop],A[curIdx])-A[dit])*(curIdx-stkTop - );
if(A[stkTop]>A[curIdx]) break;
}
stk.push(curIdx);
}
return retSum;
}
}; int main()
{
int A[]= {,,,,,,,,,,,};
Solution sol;
cout<<sol.trap(A,sizeof(A)/sizeof(int))<<endl;
return ;
}
 
 
 
 

[LeetCode] Trapping Rain Water 栈的更多相关文章

  1. [LeetCode] Trapping Rain Water 收集雨水

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

  2. [LeetCode] Trapping Rain Water II 收集雨水之二

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  3. LeetCode: Trapping Rain Water 解题报告

    https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...

  4. Leetcode: Trapping Rain Water II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  5. [leetcode]Trapping Rain Water @ Python

    原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers represe ...

  6. Leetcode Trapping Rain Water

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

  7. [LeetCode] Trapping Rain Water II 题解

    题意 题目 思路 我一开始想的时候只考虑到一个结点周围的边界的情况,并没有考虑到边界的高度其实影响到所有的结点盛水的高度. 我们可以发现,中间是否能够盛水取决于边界是否足够高于里面的高度,所以这必然是 ...

  8. leetcode Trapping Rain Water pthon

    class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 ...

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

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

随机推荐

  1. python简单实用gunicorn部署

    linux 安装 pyuthon 安装   pip install gunicorn manage.py 文件 from app import create_app app = create_app( ...

  2. js操作地址栏

    //判断地址里是否有?号,如果没有就从最后一个/截到最后,如果有?就从最后一个/截至?号处 listTable.url = location.href.lastIndexOf("?" ...

  3. stm32-IIC读写EEPROM—时序说明

    I2C 通讯协议:(Inter-Integrated Circuit)是由Phiilps 公司开发的,由于它引脚少,硬件实现简单,可扩展性强,不需要USART.CAN  等通讯协议的外部收发设备,现在 ...

  4. 大数运算:HDU-1042-N!(附N!位数的计算)

    解题心得: 这里使用了10000进制.很明显,因为是n!所以单个最大的数是10000*10000,使用万进制. 可以借鉴高精度的加法,单个乘了之后在进位. 很坑的一点,0!=1,数学不好WA了三次,尴 ...

  5. Gym 100829S_surf 动态规划的优化

    题目大意是,非你若干个任务,任务分别对应开始时间.预期收益.持续时间三项指标,让你从中选择一个受益最大的方案(没有开始时间相同的任务). 于是,标准状态转移方程应当为,设DP[K]为选择了前K个任务的 ...

  6. Tomcat之web.xml中的<url-pattern>标签

    关于web.xml配置中的<url-pattern> 标签<url-pattern> <url-pattern>是我们用Servlet做Web项目时需要经常配置的标 ...

  7. Android拨打电话不弹出系统拨号界面总结

    我在网上搜了一下,解决这个问题,有两种方式: 1.反射调用系统底层方法,并获取系统权限 反射调用的代码如下: Class phoneFactoryClass = Class.forName(" ...

  8. Java文件 ---文件相关操作

    创建文件 file.createNewFile() 注:若该文件对象未指定文件路径,则文件创建于相对路径中,即工程目录下.(“../”表示上级文件目录,相对路径前面不加“/”,eg:bin/text. ...

  9. Android 本地css引用

    /** 全局web样式 * 以前看不懂,现在仔细,耐心的看看,全懂了,认真的看一遍都懂了 * * * */ // 链接样式文件,代码块高亮的处理 public final static String ...

  10. day23 Model 操作,Form 验证以及序列化操作

    Model 操作 1创建数据库表 定制表名:       普通索引:             创建两个普通索引,这样就会生成两个索引文件   联合索引:           为了只生成一个索引文件,才 ...