(算法)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[]=;
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的更多相关文章
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [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 ...
- [LintCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- 有意思的数学题:Trapping Rain Water
LeetCode传送门 https://leetcode.com/problems/trapping-rain-water/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的 ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
随机推荐
- 压缩的问题-----WriteUp
原题:http://ctf5.shiyanbar.com/crypto/winrar/ 526172211A0700CF907300000D0000000000000056947424965E 006 ...
- hdu 5251 包围点集最小矩形 ***
题意:小度熊有一个桌面,小度熊剪了很多矩形放在桌面上,小度熊想知道能把这些矩形包围起来的面积最小的矩形的面积是多少. 求个凸包,矩形的边一定在凸包上,枚举边,求最大值,即为所求,多年不拍几何,直接套了 ...
- 保存全局Crash报告&发送邮件
上篇写到,将程序中没有处理到的crash信息保存到本地文件夹下.但是实际的情况是,你不可能总是将用户的设备拿过来.所以一般性的处理是,将crash reports发送到服务器或者邮箱.所以针对上篇的代 ...
- Py脚本运行后暂停不退出
方法一:在脚本结束后提示用户按任意键退出 import os os.system('pause') 方法二:在脚本结束后等待输入,按回车键退出 input("") 方法三:在脚本结 ...
- Java NIo 笔记001
1. Channel Channel接口只提供了两个方法: package java.nio.channels; public interface Channel { public boolean i ...
- Jmeter+JDK的安装学习笔记
第一步:首先从jmeter的官网下载jmeter,目前最新版本为3.3,支持的JDK最高为1.8 下载地址: jmeter:http://jmeter.apache.org/download_jmet ...
- 读书笔记_Effective_C++_条款三十二:确定你的public继承继承塑模出is-a关系
这一条款是说的是公有继承的逻辑,如果使用继承,而且继承是公有继承的话,一定要确保子类是一种父类(is-a关系).这种逻辑可能与生活中的常理不相符,比如企鹅是生蛋的,所有企鹅是鸟类的一种,直观来看,我们 ...
- Linux 内核编译步骤及配置详解
前言 Linux内核是操作系统的核心,也是操作系统最基本的部分. Linux内核的体积结构是单内核的.但是他充分采用了微内核的设计思想.使得虽然是单内核.但工作在模块化的方式下.并且这个模块可以 ...
- C++反汇编-结构体和类
学无止尽,积土成山,积水成渊-<C++反汇编与逆向分析技术揭秘> 读书笔记 对象的内存布局 一般计算公式: 对象内存大小 = sizeof(数据成员1)+ sizeof(数据成员2) +. ...
- P2P通信原理与实现(C++)
1.简介 当今互联网到处存在着一些中间件(MIddleBoxes),如NAT和防火墙,导致两个(不在同一内网)中的客户端无法直接通信.这些问题即便是到了IPV6时代也会存在,因为即使不需要NAT,但还 ...