(算法)Trapping Rain Water I

题目:

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[0]=0;
    for(int i=1;i<sz;i++){
        if(water[i]>preMaxWater[i-1])
            preMaxWater[i]=water[i];
        else
            preMaxWater[i]=preMaxWater[i-1];
    }

    vector<int> sufMaxWater(sz);
    sufMaxWater[sz-1]=0;
    for(int i=sz-2;i>=0;i--){
        if(water[i]>sufMaxWater[i+1])
            sufMaxWater[i]=water[i];
        else
            sufMaxWater[i]=sufMaxWater[i+1];
    }

    int sum=0;
    for(int i=0;i<sz;i++){
        sum+=max(0,min(preMaxWater[i],sufMaxWater[i])-water[i]);
    }    

    return sum;
}

int main(){
    int n;
    while(cin>>n){
        vector<int> water(n,0);
        for(int i=0;i<n;i++)
            cin>>water[i];

        cout << MaxTrappingWater(water) <<endl;
    }

    return 0;
}
时间: 2024-08-28 20:56:28

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

每日算法之三十三:Trapping Rain Water

这是一个很有意思的问题,求解最大容积问题,值得动脑筋想一想. 原题如下: 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 ab

Twitter算法面试题详解(Java实现)——Trapping Rain Water

[算法题:求出高低不同的砖中能存多少水] 「Trapping Rain Water」 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.

有意思的数学题:Trapping Rain Water

LeetCode传送门 https://leetcode.com/problems/trapping-rain-water/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的部分 假设:积木宽度均为1 输入:各个积木的高度 输出:所有积木能容纳水的“面积” 思考过程 1. 逐一求积木的间隔似乎不太容易.特别对于图中3-7积木间的容积,如果可以先求底部(4-6间)的容积,当求解上层(3-7)的容积时,还需要做额外的处理,如减掉底部的高度. 2. 既然如此,可否先求出3-7

[LeetCode][JavaScript]Trapping Rain Water

Trapping Rain Water 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

[LintCode] Trapping Rain Water II

Trapping Rain Water II Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1 x 1, compute how much water it is able to trap after raining. Example Given 5*4 matrix [12,13,0,12] [13,4,13,12] [13,8,10,12] [

Leetcode 407. Trapping Rain Water II

Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining. Note:Both m and n are less than 110. The height of each unit cell is greater tha

Trapping Rain water 诸多细节须注意

Trapping Rain Water Total Accepted: 35650 Total Submissions: 118319My Submissions Question Solution 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.

LeetCode 042 Trapping Rain Water

题目要求:Trapping Rain Water 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 elevatio

[leetcode]Trapping Rain Water @ Python

原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: 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,