题目:

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.

思路:

题目的意思是说,给定一非负的整数数组,数组的每个数字表示柱子的高度,如果把这些柱子组成一个容器,最多能盛多少水?

思路是这样的,每个柱子(高度为h)所在的位置能够装的水的容量取决于它前面所有柱子的最高高度preHeight以及它后面所有柱子的最高高度postHeight,

即装水的容量等于max(0,min(preHeight-postHeight)-h);

因此可以通过计算给定数组的前缀数组的最大值(如preMax[i]表示数组从0到i-1位置的最大值);以及后缀数组的最大值(如postMax[i]表示数组从i到n-1位置的最大值),就可以利用上面的公式计算每个位置的水容量,最后加起来就是总共的容量。

代码:

#include<iostream>
#include<vector>
#include<stdlib.h> using namespace std; int MaxTrappingWater(const vector<int> &water){
int sz=water.size(); vector<int> preMaxWater(sz);
preMaxWater[]=;
for(int i=;i<sz;i++){
if(water[i]>preMaxWater[i-])
preMaxWater[i]=water[i];
else
preMaxWater[i]=preMaxWater[i-];
} vector<int> sufMaxWater(sz);
sufMaxWater[sz-]=;
for(int i=sz-;i>=;i--){
if(water[i]>sufMaxWater[i+])
sufMaxWater[i]=water[i];
else
sufMaxWater[i]=sufMaxWater[i+];
} int sum=;
for(int i=;i<sz;i++){
sum+=max(,min(preMaxWater[i],sufMaxWater[i])-water[i]);
} return sum;
} int main(){
int n;
while(cin>>n){
vector<int> water(n,);
for(int i=;i<n;i++)
cin>>water[i]; cout << MaxTrappingWater(water) <<endl;
} return ;
}

(算法)Trapping Rain Water I的更多相关文章

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

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

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

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

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

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

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

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

  5. [LeetCode] 42. Trapping Rain Water 收集雨水

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

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

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

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

  8. LeetCode - 42. Trapping Rain Water

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

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

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

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

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

随机推荐

  1. 【20181102T1】优美的序列【二分+ST表】

    题面 [正解] 相当于是 \(GCD_{i=L}^{R} A_i = min_{i=L}^{R} \{A_i\}\) 然后GCD可以用ST表实现\(O(log A_i)\)查询 并且GCD是递减的,所 ...

  2. [CodeChef-QUERY]Observing the Tree

    题目大意: 给你一棵树,一开始每个点的权值都是0,要求支持一下三种操作: 1.路径加等差数列. 2.路径求和. 3.回到以前的某次操作. 强制在线. 思路: 树链剖分+主席树. 最坏情况下,n个点的树 ...

  3. Sublime Text2 默认语言(windows/unix)设置,Sublime插件大全

    Sublime默认系统语言设置 Sublime Text 2默认使用的就是UTF8,这个UTF8模式使用的是不带BOM的,如果要修改这个配置,到Perference->Settings-User ...

  4. mysql 审计插件编写

    http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=1864367&page=1#pid13527550 http://blog ...

  5. cocos2d-x 3.0 将cpp-tests编译成Android版本号APK文件

    cmd模式 进入到 E:\cocos2d-x-3.0rc1\cocos2d-x-3.0rc1\build 输入命令 android list targets 在输入: android-build.py ...

  6. 使用jQuery异步传递含复杂属性及集合属性的Model到控制器方法

    Student类有集合属性Courses,如何把Student连同集合属性Courses传递给控制器方法?     public class Student     {         public ...

  7. MVC二级联动使用$.getJSON方法

    本篇使用jQuery的$.getJSON()实现二级联动.   □ View Models 1: namespace MvcApplication1.Models 2: { 3: public cla ...

  8. 建议:一般地,建议使用xcode 4.3开发app 而不是使用xcode4.5

    建议:一般地,建议使用xcode 4.3开发app 而不是使用xcode4.5 因为,xcode4.5 默认的sdk 是ios6.0,这里面有很多新特性.比如,Autolayout 等,这个特性再io ...

  9. spring事務

    spring事物 spring事物其实就是对数据库事物的一种支持,没有数据库事物的话,spring本身是不能提供事物支持的: 在最开始使用原始的jdbc连接数据库进行炒操作是, 获取连接后可以使用co ...

  10. Unity3.x游戏开发经典教程 书例 100%完毕~

    大家都公布自己的作品,作为一个新人,我也发点什么.刚刚做完了Unity3.x游戏开发经典教程书例不久,假如有同学想学这本书入门U3D,我的作品也能让新人參考一下...脚本都是C#写的.以下附上链接~ ...