[Leetcode]Trapping Rain Water - 递减栈
来源:互联网 发布:淘宝好友动态在哪里 编辑:程序博客网 时间:2024/05/22 08:18
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.
解题思路:时刻维护一个递减的栈。
1.当前元素比栈顶元素小时,直接进栈;
2.当前元素大于等于栈顶元素时,弹出栈顶元素,由弹出后新的栈顶元素与当前元素构成一个凹槽,
但凹槽并不一定是见底的。比如4、2、3,其中的4和3形成一个凹槽,
但因为中间有个2,所以只能容纳1个单位的水。
这也就是min(A[k],A[i])-t的原因,t就是这个2,A[k]=4,A[i]=3。
3.将每次出栈产生的结果加到一起就行了。
class Solution {public: int trap(int A[], int n) { int top=0; if(n<3) return 0; s[top++]=0; int res=0,t=0,pre=0; for(int i=1;i<n;i++) { while(top>0&&A[i]>=A[s[top-1]]) { t=A[s[--top]];//栈顶元素,能进到这层循环里,说明A[i]比栈顶元素要大 if(top>0)//弹出刚刚的栈顶元素后,那这次栈顶的元素一定比刚刚弹出的那个大 { pre=s[top-1];//新的栈顶元素 res+=(i-pre-1)*(min(A[k],A[i])-t);//这样新栈顶元素与A[i]这间就形成了一个凹槽 } } s[top++]=i; } return res; }private: int s[100000];};
0 0
- [Leetcode]Trapping Rain Water - 递减栈
- LeetCode: Trapping Rain Water
- LeetCode : Trapping Rain Water
- [LeetCode] Trapping Rain Water
- [Leetcode] Trapping Rain Water
- [Leetcode] Trapping Rain Water
- [LeetCode]Trapping Rain Water
- LeetCode-Trapping Rain Water
- [leetcode] Trapping Rain Water
- [LeetCode] Trapping rain water
- Leetcode: Trapping Rain Water
- Leetcode:Trapping Rain Water
- [LeetCode] Trapping Rain Water
- <Leetcode>Trapping Rain Water
- Leetcode Trapping Rain Water
- leetcode Trapping Rain Water
- leetcode Trapping Rain Water
- LeetCode Trapping Rain Water
- linux 文件移动
- installing vmware tools: the path is not valid path to the kernel headers终极解决方案
- .net razor引擎模板语法
- MongoDB基本使用
- 如何使用cocos2d-x制作一个塔防游戏:引子
- [Leetcode]Trapping Rain Water - 递减栈
- stl仿函数
- SQL Server日期时间格式转换字符串详解
- iOS 用其它应用打开文件
- 中心关头词的取舍技巧
- 如何使用cocos2d-x来制作一个塔防游戏:第一部分
- 简单的INotifyPropertyChanged接口使用的示例类
- ios+oc面试题
- jdk环境变量的配置