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(vector<int>& height) {
int n=height.size();
int result=;
vector<int> left(n),right(n);
for(int i=;i<n;i++)
{
left[i]=i?max(left[i-],height[i]):height[i];
}
for(int j=n-;j>=;j--)
{
right[j]=(n--j)?max(right[j+],height[j]):height[j];
}
for(int k=;k<n;k++)
{
result+=min(left[k],right[k])-height[k];
}
return result;
}
};

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:Container With Most Water,Trapping Rain Water

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

  5. LeetCode - 42. Trapping Rain Water

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

  6. 有意思的数学题:Trapping Rain Water

    LeetCode传送门 https://leetcode.com/problems/trapping-rain-water/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的 ...

  7. [Leetcode][Python]42: Trapping Rain Water

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...

  8. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

  9. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

  10. 【LeetCode】42. Trapping Rain Water

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

随机推荐

  1. 【java】关于时间

    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Created b ...

  2. c语言期末复习题

    代码参考:<K&R> 1.单词计数 #include<stdio.h> #define IN 1 #define OUT 0 main() { int c, state ...

  3. odoo定时任务

    python代码 # -*- encoding: utf-8 -*- from openerp.osv import fields, osv, orm import logging _logger = ...

  4. [SoapUI] SoapUI Response 格式控制

    application/后面可以修改为自己所需要的内容格式.

  5. zabbix3.0.4 部署之五 (LNMP > nginx1.9.9)

    1 wget http://nginx.org/download/nginx-1.9.9.tar.gz cd /opt/pcre-8.35 autoreconf -ivf Installing sha ...

  6. Java队列工具类(程序仅供练习)

    public class QueueUtils<T> { public int defaultSize; public Object[] data; public int front = ...

  7. Aptana Studio 2启动时提示 Workspace Cannot Be Created 解决办法

    今天在安装Aptana Studio 2时出现这个东东,卸载后再安装依旧不行最后找到原因 原因 : 就是由于你把“我的文档”的位置修改造成的. 但Aptana还以为 “我的文档”的位置 是在系统的默认 ...

  8. js问题解释

    今天群里有人问一个js问题,现列出以便其他人参考. Function.prototype.curry=function(){ var slice=Array.prototype.slice, args ...

  9. HTML“计算机输出”标签 <code><kbd><samp><tt><var><pre>

    我们并不反对使用它们,但是如果您只是为了达到某种视觉效果而使用这些标签的话,我们建议您使用样式表,那么做会达到更加丰富的效果. <code> 标签-定义计算机代码文本. 定义和用法: &l ...

  10. Java 设计模式泛谈&装饰者模式和单例模式

    设计模式(Design Pattern) 1.是一套被反复使用.多人知晓的,经过分类编目 的 代码设计经验总结.使用设计模式是为了可重用代码,让代码更容易维护以及扩展. 2.简单的讲:所谓模式就是得到 ...