(算法)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 ...
随机推荐
- Windows 0day成功验证之ETERNALBLUE
本帖由春秋首发~作者:神风 @春秋文阁负责人 方程式又一波0day[该贴有工具]:https://bbs.ichunqiu.com/thread-21736-1-1.html 最近一段时间出现一波高潮 ...
- BZOJ 1854: [Scoi2010]游戏 并查集
1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 2672 Solved: 958[Submit][Status][ ...
- jProfiler远程连接Linux监控jvm1运行状态
第一步:下载软件官网地址:https://www.ej-technologies.com/download/jprofiler/files,下载一个linux服务端,一个windows客户端 GUI界 ...
- Qt 4.7.2移植到ARM教程
Qt的移植包括步骤如下: 1.下载并安装VMware WorkStation 12(最好比较高级的版本,早期的版本共享目录在虚拟机里可能显 示不了). 2.下载ubuntu 14.0.4(最好是lts ...
- 华为S5300系列交换机V100R005SPH020升级补丁
S23_33_53-V100R005SPH020.pat 附件: 链接:https://pan.baidu.com/s/1-qgNEtRsZbNnC4eK4DTctA 密码:wpn3
- WPF中删除打开过的图片
在WPF中,当我们删除打开过的图片时,往往会遇到"...无法删除,文件正在被另一个进程使用"的异常.即使当前文件是打开后关闭过的也不行. 这个问题的原因很简单,是因为WPF的缓存策 ...
- 5V and 3V Level Translators
http://www.daycounter.com/Circuits/Level-Translators/Level-Translators.phtml Interfacing 5V and 3V l ...
- PostgreSQL SystemTap on Linux 转
PostgreSQL 支持动态跟踪, 可以通过dtrace或者systemtap工具统计相关探针的信息. 安装systemtap yum install systemtap kernel-debugi ...
- Nucleus PLUS任务调度
概述 Nucleus Plus内核(Kernel)的主要目的是管理实时任务的竞争执行(共享CPU),为应用提供各种便利,高速响应外部事件.Nucleus Plus的系统结构如图1所看到的,能够看出线程 ...
- Linux下open与fopen的区别
int open(const char *path, int access,int mode) path 要打开的文件路径和名称 access 访问模式,宏定义和含义如下: ...