题目:

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. LOJ P1155 双栈排序 二分图染色 图论

    https://www.luogu.org/problem/show?pid=P1155 题解: https://www.byvoid.com/zhs/blog/noip2008-twostack 开 ...

  2. HTML5区块和大纲算法

    原文链接: Using HTML sections and outlines - Mozilla Developer Network 每集HTML5+CSS3网页布局教程-2大纲算法 HTML5标准带 ...

  3. python开发_csv(Comma Separated Values)_逗号分隔值_常用导入导出格式_完整版_博主推荐

    ## 最近出了一趟差,是从20号去的,今天回来...# 就把最近学习的python内容给大家分享一下...#''' 在python中,CSV(Comma Separated Values),从字面上面 ...

  4. BZOJ 1029: [JSOI2007]建筑抢修 优先队列

    1029: [JSOI2007]建筑抢修 Time Limit: 4 Sec  Memory Limit: 162 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...

  5. JS对象与PHP对象的对比

    一.对象的创建与访问 1.JS对象的创建与访问 //方式一(通过内置构造函数创建后添加属性) var obj = new Object(); obj.name = 'Lucy'; //通过点添加属性 ...

  6. XStream转换Java对象与XML

    1.引入需要的jar包,在pom.xml中配置依赖 <dependency> <groupId>com.thoughtworks.xstream</groupId> ...

  7. jrebel使用

    背景与愿景:开发环境下,tomcat对热布署的支持还不够全面,致使开发人员浪费大量时间在重起服务上.为了提高开发效率,决定引入Jrebel,它对热布署的支持相对比较全面.虽然Jrebel官方号称使用它 ...

  8. 点赞和吐糟Adblock Plus~进阶教程

    前言:Adblock Plus后文都简称ABP,这是一篇ABP进阶教程!用ABP实现flashBlock和NoScript.推荐有相当基础的阅读.刚開始学习的人先看懂这里:http://adblock ...

  9. 用最简单的例子理解单例模式(Singleton Pattern)

    当从应用程序全局的角度来看,如果只允许类的一个实例产生,就可以考虑单例模式. □ 即时加载的单例模式 把类的实例赋值给类的一个静态字段. class Program { static void Mai ...

  10. 《MacTalk·人生元编程》

    <MacTalk·人生元编程> 基本信息 <MacTalk·人生元编程> 基本信息 作者:池建强 出版社:人民邮电出版社 ISBN:9787115342232 上架时间:201 ...