Trapping Rain Water
2015-07-08 22:28
351 查看
Question:
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
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!
Solution:
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!
Solution:
class Solution { public: int trap(vector<int>& height) { int n=height.size(); int sum=0; int *max_left=new int ; int *max_right=new int ; max_left[0]=0; max_right[n-1]=0; for(int i=1;i<n-1;i++) { max_left[i]=max(max_left[i-1],height[i-1]); max_right[n-1-i]=max(max_right[n-i],height[n-i]); } for(int j=1;j<n-1;j++) { int val=min(max_left[j],max_right[j]); if(val>height[j]) sum+=val-height[j]; } return sum; } };
相关文章推荐
- Xamarin.Android开发实践(十三)
- Objective-C(十五、内存管理,MRC,循环引用,dealloc和setter方法)——iOS开发基础
- [精通iOS开发(第5版)]第15章 Grand Central Dispatch和后台处理笔记
- iOS 圆角矩形圆角设置
- BeanFactory和ApplicationContext的作用和区别
- 资江热敏打印二维码指令android
- OC基础8:分类和协议
- Unity4.x升级5.0出现的32位dll问题
- 如何由新特性跳转到App首页
- android之3g调试1
- 【Cocos2d-x】3.6 Android环境搭建
- Android 方向传感器与磁力计和加速度传感器之间的关系
- iOS 中函数和方法的区别
- Android图片适配,drawable文件夹,低分辨率图片是否必要
- VS2012导入COCOSx-2d 3.6项目的过程总结
- VS2012导入COCOSx-2d 3.6项目的过程总结 分类: cocos2d安装设置 2015-07-08 21:59 12人阅读 评论(0) 收藏
- android笔记
- [cocos2dx] cocosdx编译工程那些事
- Object-c中的点语法
- 新建cocos2dx开发遇到问题及解决办法