leetCode_Trapping Rain Water
来源:互联网 发布:阿里云可以干嘛 编辑:程序博客网 时间:2024/05/22 01:38
题意:给定n个非负数代表每个柱子的高度,宽度均为1。问能在空间里放多少水。
解法:设定previous与next,然后移动next,找到第一个比prevoius高的或者比prevoius低的柱子当中最高的。然后两者较低的乘以宽度-所有柱子的高度之和即为prevoius,next之间的蓄水量。然后让prevoius=next,依次循环。挺虐的,做了小半天才AC。
代码:
int trap(vector<int>& height){ int i=0,j,ans=0,size=height.size(),has=0,tsum=0,r=0,ti,pre,next,tmax; if(height.size()==0) return 0; while(height[i]==0) i++; pre=i; next=i+1; while(pre<size-1) { tsum=height[pre]; tmax=-1; while(height[next-1]>height[next]&&next<size) { tsum=tsum+height[next]; next++; } if(next>=size) break; while(next<size) { tsum=tsum+height[next]; if(height[next]>=height[pre]) { tmax=height[next]; ti=next; break; } else { if(tmax<height[next]) { tmax=height[next]; ti=next; } } next++; } if(height[ti]>=height[pre])ans=ans+min(height[pre],height[ti])*(ti-pre+1)-tsum+abs(height[pre]-height[ti]); else for(i=pre;i<=ti;i++) if(height[i]<height[ti]) ans+=height[ti]-height[i]; pre=ti; next=ti+1; } return ans;}
0 0
- LeetCode_Trapping Rain Water
- leetcode_Trapping Rain Water
- leetCode_Trapping Rain Water
- Leetcode_trapping-rain-water(updated c++ and python version)
- 【Leetcode】Traping Rain Water (Water)
- LeetCode: Trapping Rain Water
- LeetCode : Trapping Rain Water
- Trapping Rain Water
- [LeetCode] Trapping Rain Water
- [Leetcode] Trapping Rain Water
- [Leetcode] Trapping Rain Water
- Trapping Rain Water
- Trapping Rain Water
- [LeetCode]Trapping Rain Water
- Trapping Rain Water
- Trapping Rain Water
- LeetCode-Trapping Rain Water
- Trapping Rain Water
- fresco源码分析-软引用的黑科技
- PostgreSQL源码目录结构及其相关模块功能分析
- call() apply() 定义一些函数,用于传递和调用
- 在Eclipse中查看JDK类库的源代码
- Ajax学习
- leetCode_Trapping Rain Water
- JS、Jquery选择器总结
- unity android SDK 的安装
- C++随机整数
- APP架构设计
- 前端mvvm框架vuejs教程集合
- POJ 1228 Grandpa's Estate (凸包、保留凸包边上的点)
- 技巧:全选按钮(选中和非选中)实现方法
- 关于RemoteView的一点愚见(RemoteView在AppWidget中的工作流程)