Trapping Rain Water
来源:互联网 发布:明源软件股份有限公司 编辑:程序博客网 时间:2024/06/11 23:38
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: /** * @param heights: a vector of integers * @return: a integer */ int trapRainWater(vector<int> &heights) { // write your code here int n = heights.size(); if (n < 1) { return 0; } int result = 0; int rightMax[n]; rightMax[n-1] = heights[n-1]; for (int i = n-2; i >= 0; i--) { if (heights[i] > rightMax[i+1]) { rightMax[i] = heights[i]; } else { rightMax[i] = rightMax[i+1]; } } int begin = 0; while (begin < n && heights[begin] == 0) { begin++; } for (int i = begin+1; i < n; i++) { if (heights[i] >= heights[begin] || heights[i] == rightMax[i]) { int top = min(heights[i], heights[begin]); for (int j = begin+1; j < i; j++) { if (heights[j] < top) { result += (top - heights[j]); } } begin = i; } } return result; }};
0 0
- 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
- [leetcode] Trapping Rain Water
- Trapping Rain Water
- [LeetCode] Trapping rain water
- Leetcode: Trapping Rain Water
- 38 - Trapping Rain Water
- Expected positional parameter count: 1, actual parameters: [] [{ call proc1(?)}]
- uvalive5721 Activation(概率dp)
- 黑马程序员——高新技术——反射2(指定类中的方法)
- 二级导航
- Named query not known: *
- Trapping Rain Water
- 黑马程序员——java基础——正则表达式
- [1]_技术研发 - 七年之痒
- BUG现象与对应0003 全局数据结构复杂多线程中应该被复制使用
- C++ 常用代码001 IsValidFileName VC/MFC 判断有效文件名
- js中字符串操作函数
- Photoshop 标注图像
- 如何测试写磁盘的速度?
- 的更加廣泛的房貸和規範的方式