LeetCode_Trapping Rain Water
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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n <= ) return ;
vector<int> left(n,);
vector<int> right(n, );
left[] = A[];
for(int i =; i < n;++i)
{
left[i] = left[i-] > A[i] ? left[i-] : A[i];
}
right[n-] = A[n-];
for(int i = n-; i>= ;--i){
right[i] = right[i+] > A[i] ? right[i+] : A[i];
}
int res = ;
for(int i = ; i< n; i++){
int min = left[i] < right[i]? left[i] : right[i];
res += min - A[i];
}
return res;
}
};
LeetCode_Trapping Rain Water的更多相关文章
- [LeetCode] Trapping Rain Water II 收集雨水之二
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LintCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- Leetcode Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 42. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- Leetcode: Trapping Rain Water II
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
随机推荐
- Java正则表达式的语法与示例
Java正则表达式的语法与示例 java 正则表达式 正则表达式语法 java正则表达式语法 java正则表达式 概要: Java正则表达式的语法与示例 | |目录 1匹配验证-验证Email是否正确 ...
- android 在EditText中显示表情图片
public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { su ...
- HDU4432 Sum of Divisors
涉及知识点: 1. 进制转换. 2. 找因子时注意可以降低复杂度. Sum of divisors Time Limit: 2000/1000 MS (Java/Others) Memory L ...
- 【转】windows下vs2008/2010+opencv2.2开发环境搭建
版权声明:本文为博主原创文章,未经博主允许不得转载. 1.下载安装Cmake 2.用cmake配置opencv2.2,然后编译,安装 3. 在vs2008中配置opencv2.2 4.Demo 1.下 ...
- JS浏览器对象-History对象
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Wamp集成环境配置多站点
一.打开apache配置文件httpd.conf 二.修改httpd.conf配置文件 1.在配置文件httpd.conf中搜索 conf/extra/httpd-vhosts.conf,然后将该行代 ...
- iphone开发中数据持久化之——嵌入式SQLite(三)
前两篇分别讨论了使用属性列表的数据持久化.使用对象归档的数据持久化,本文将讨论第三个实现数据持久化的方法---嵌入式SQL数据库SQLite3.SQLite3在存储和检索大量数据方面非常有效.它还能够 ...
- Android 推断当前Activity是不是最后一个Activity 以及 应用或Activity是否存在
推断当前Activity是最后一个Activity: 在Activity的方法中, 有一个方法isTaskRoot()方法, 这种方法能够推断当前Activity是否是最后一个Activity, 假 ...
- license文件生成原理
byte解密weblogic加密oraclehex 现在很多J2EE应用都采用一个license文件来授权系统的使用,特别是在系统购买的早期,会提供有限制的license文件对系统进行限制,比如试 ...
- Excel02-快速无误输入多个零
第一步:设置单元格格式-->小数位数为0,货币符号为¥ 第二步:在单元格输入数据:1**5回车即显示为¥100,000 **N 表示后面有N个零,会自动加入我们设置的货币符号¥ 这对我们在输入巨 ...