LEETCODE: Trapping Rain Water
来源:互联网 发布:永恒之塔模拟器源码 编辑:程序博客网 时间:2024/06/07 17:36
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!
class Solution {public: int trap(int A[], int n) { int maxi = 0; for(int ii = 1; ii < n; ii ++) { if(A[maxi] < A[ii]) { maxi = ii; } } int result = 0; // Left to max int maxval = A[0]; for(int ii = 1; ii < maxi; ii ++) { if(A[ii] > maxval) { maxval = A[ii]; } else { result += maxval - A[ii]; } } // Right to max maxval = A[n - 1]; for(int ii = n -2; ii > maxi; ii --) { if(A[ii] > maxval) { maxval = A[ii]; } else { result += maxval - A[ii]; } } return result; }};
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
- java自学日记二
- CSS3 Transition
- grub2 设置启动引导 Fedora 21 Workstation Live iso
- Eclipse混淆文件导入Android Studio Gradle编译报input jar file is specified twice
- CSS3 Transform
- LEETCODE: Trapping Rain Water
- 用win7搭建基于IIS的web服务器
- PCI9052使用理解及与DSP6713的接口
- SQLiteDatabase中query、insert、update、delete方法参数说明 (2012-08-10 10:30:41)转载▼ 标签: android database sqlite
- 转:标准C++中的string类的用法总结
- CSS3 Animation
- Mysql启动时出现1067,进程意外终止解决方法
- 常用Eclipse快捷键
- UVa 455 Periodic Strings(周期字符串)