[LeetCode] Trapping Rain Water 栈
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!
好开心,逻辑这么麻烦的题目,写了一次没有错,提交了直接过。


#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 栈的更多相关文章
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [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 ...
- LeetCode: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...
- 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 ...
- [leetcode]Trapping Rain Water @ Python
原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers represe ...
- Leetcode Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] Trapping Rain Water II 题解
题意 题目 思路 我一开始想的时候只考虑到一个结点周围的边界的情况,并没有考虑到边界的高度其实影响到所有的结点盛水的高度. 我们可以发现,中间是否能够盛水取决于边界是否足够高于里面的高度,所以这必然是 ...
- leetcode Trapping Rain Water pthon
class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
随机推荐
- python简单实用gunicorn部署
linux 安装 pyuthon 安装 pip install gunicorn manage.py 文件 from app import create_app app = create_app( ...
- js操作地址栏
//判断地址里是否有?号,如果没有就从最后一个/截到最后,如果有?就从最后一个/截至?号处 listTable.url = location.href.lastIndexOf("?" ...
- stm32-IIC读写EEPROM—时序说明
I2C 通讯协议:(Inter-Integrated Circuit)是由Phiilps 公司开发的,由于它引脚少,硬件实现简单,可扩展性强,不需要USART.CAN 等通讯协议的外部收发设备,现在 ...
- 大数运算:HDU-1042-N!(附N!位数的计算)
解题心得: 这里使用了10000进制.很明显,因为是n!所以单个最大的数是10000*10000,使用万进制. 可以借鉴高精度的加法,单个乘了之后在进位. 很坑的一点,0!=1,数学不好WA了三次,尴 ...
- Gym 100829S_surf 动态规划的优化
题目大意是,非你若干个任务,任务分别对应开始时间.预期收益.持续时间三项指标,让你从中选择一个受益最大的方案(没有开始时间相同的任务). 于是,标准状态转移方程应当为,设DP[K]为选择了前K个任务的 ...
- Tomcat之web.xml中的<url-pattern>标签
关于web.xml配置中的<url-pattern> 标签<url-pattern> <url-pattern>是我们用Servlet做Web项目时需要经常配置的标 ...
- Android拨打电话不弹出系统拨号界面总结
我在网上搜了一下,解决这个问题,有两种方式: 1.反射调用系统底层方法,并获取系统权限 反射调用的代码如下: Class phoneFactoryClass = Class.forName(" ...
- Java文件 ---文件相关操作
创建文件 file.createNewFile() 注:若该文件对象未指定文件路径,则文件创建于相对路径中,即工程目录下.(“../”表示上级文件目录,相对路径前面不加“/”,eg:bin/text. ...
- Android 本地css引用
/** 全局web样式 * 以前看不懂,现在仔细,耐心的看看,全懂了,认真的看一遍都懂了 * * * */ // 链接样式文件,代码块高亮的处理 public final static String ...
- day23 Model 操作,Form 验证以及序列化操作
Model 操作 1创建数据库表 定制表名: 普通索引: 创建两个普通索引,这样就会生成两个索引文件 联合索引: 为了只生成一个索引文件,才 ...