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的更多相关文章

  1. [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 ...

  2. [LeetCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  3. [LintCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  4. Leetcode Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  5. 42. Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  6. Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  7. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  8. LeetCode - 42. Trapping Rain Water

    42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...

  9. 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 ...

随机推荐

  1. BZOJ1697: [Usaco2007 Feb]Cow Sorting牛排序

    1697: [Usaco2007 Feb]Cow Sorting牛排序 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 387  Solved: 215[S ...

  2. 设计模式 ( 十八 ):State状态模式 -- 行为型

    1.概述 在软件开发过程中,应用程序可能会根据不同的情况作出不同的处理.最直接的解决方案是将这些所有可能发生的情况全都考虑到.然后使用if... ellse语句来做状态判断来进行不同情况的处理.但是对 ...

  3. java--对象比较器

    在实际的项目中,经常会遇到排序问题,对于基本数据类型java支持Arrays.sort()和Collection.sort()对集合进行排序,但是对用户自定义类型排序呢?java给我们提供了两种解决方 ...

  4. EF并发性能文章

    http://www.cnblogs.com/farb/p/ConcurrencyAndTransctionManagement.html

  5. DedeCMS官方手册

    DedeCMSV5.3使用手册 DedeCMSV57数据库结构文档 Dedecms 文件目录结构

  6. SQL Server 2008 导出数据与导入数据任务介绍

    一. 实例数据库介绍 源数据库Test_Other_DB:存在tb_Class,tb_Student,tb_TestTable三张表. 目标数据库TestDB_Output:空库,不含任何表. 二. ...

  7. [每日一题] OCP1z0-047 :2013-08-15 描述GROUPING 函数 .......................................43

    正确答案:C ,否则返回0. 官方解释:GROUPING distinguishes superaggregate rows fromregular grouped rows. GROUP BY ex ...

  8. 用GDB调试多进程程序

    在子进程中sleep.然后attach上去. gdb --pid=123456 ps出子进程的id,gdb attach 进程号. http://www.ibm.com/developerworks/ ...

  9. 自定义控件(视图)2期笔记01:自定义控件之自定义View的步骤

    1. 根据Android Developers官网的介绍,自定义控件你需要以下的步骤: (1)创建View (2)处理View的布局 (3)绘制View (4)与用户进行交互 (5)优化已定义的Vie ...

  10. LDAP缓存命令

    启动cacao及实例: [root@rusky bin]# cd /home/ldap/iamldap/dsee6/cacao_2/cacao/bin [root@rusky bin]# ./caca ...