题目:

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. chinese hacker-----WriteUp

    原题地址:http://ctf5.shiyanbar.com/web/2/ 提示下载一个数据库 下载下来后发现是加密的  有密码,但发现密码不是4648 这里用到“DbView” 直接破解密码进入 发 ...

  2. 洛谷OJ P2356 弹珠游戏 维护前缀和

    题目描述 Description MedalPluS 和他的小伙伴 NOIRP 发掘了一个骨灰级别的游戏--超级弹珠. 游戏的内容是:在一个 n*n 的矩阵里,有若干个敌人,你的弹珠可以摧毁敌人,但只 ...

  3. web开发中兼容性问题(IE8以上含)持续更新~~

    在实际开发中总是遇到莫名其妙的问题~~~那么就记录下来这些问题,对这些问题进行一个总结. 1.事件对象 1)事件参数e,就是事件对象,标准的获取方式 2)e.eventPhase 事件阶段,IE8以前 ...

  4. service redis does not support chkconfig 的解决办法

    问题解决办法如下: 必须把下面两行注释放在/etc/init.d/redis文件靠前的注释中(加入以下注释): # chkconfig: # description: Redis is a persi ...

  5. php curl伪造referer

    CURL方式: SOCKET方式: file_get_contents方法: 通过上面的代码,我们就把referer地址伪装为http://www.xxxx.com,你可以写一段代码: $_SERVE ...

  6. STM32 100 pin 多个外设译码方案 - SN74LVC1G29

    http://www.ti.com/lit/ds/symlink/sn74lvc1g29.pdf

  7. PostgreSQL 资源

    http://blog.163.com/digoal@126/blog/static/163877040201172183022203/ http://m.oschina.net/u/2426299? ...

  8. Windows Phone Silverlight 8.1 apps

    The Windows Phone Silverlight 8.1 app model gives Windows Phone 8 developers access to some of the n ...

  9. hibernate一级缓存,二级缓存和查询缓存

    一级缓存 (必然存在)  session里共享缓存,伴随session的生命周期存在和消亡:   1. load查询实体支持一级缓存 2. get查询实体对象也支持 3. save保存的实体对象会缓存 ...

  10. Phone重绘机制drawRect 转

    Phone重绘机制drawRect 如何使用iPhone进行绘图.重绘操作iPhone的绘图操作是在UIView类的drawRect方法中完成的,所以如果我们要想在一个UIView中绘图,需要写一个扩 ...