[LeetCode] 42. 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 is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
问题: 给定一个数组,每个元素表示海报高度,每个元素宽度均为 1 ,求这个数组能装多少雨水。
虽然这道题属于 two pointers 类型,不过我是用借助队列来解的。
可能是因为之前做过直方图相关的题目Largest Rectangle in Histogram ,这道题做起来感觉思路挺顺畅。将数组的值称为泥土体积。
- 对于从左往右的递增区间,装满雨水后的总体积 - 该区间的总泥土体积 = 该区间的总雨水
- 对于剩余的从右往左的递增区间,同样地,装满雨水后的总体积 - 该区间的总泥土体积 = 该区间的总雨水
对于题目中的例子而言,从左往右的递增区间是 [0,1,0,2,1,0,1,3 ], 剩余的从右往左的递增区间是 [ 3,2,1,2,1]。
class place{
public:
int idx;
int height;
place(int idx, int height){
this->idx = idx;
this->height = height;
}
};
int trap(vector<int>& height) {
if(height.size() == ){
return ;
}
// 计算从左往右的递增区间
queue<place*> qL;
place* tmpp = new place(, height[]);
qL.push(tmpp);
for (int i = ; i < height.size(); i++) {
if (height[i] >= qL.back()->height) {
place* tmpp = new place(i, height[i]);
qL.push(tmpp);
}
}
int totalRectL = ;
while (qL.size() > ) {
place* tmpp = qL.front();
qL.pop();
int len = qL.front()->idx - tmpp->idx;
totalRectL += (tmpp->height * len);
}
place* heighestL = qL.front();
qL.pop();
int earthAmtL = ;
for (int i = ; i < heighestL->idx; i++) {
earthAmtL += height[i];
}
int waterL = totalRectL - earthAmtL;
// 计算剩余的从右往左的递增区间
queue<place*> qR;
tmpp = new place((int)height.size()-,height[height.size()-]);
qR.push(tmpp);
for (int i = (int)height.size()-; i >= heighestL->idx; i--) {
if (height[i] >= qR.back()->height) {
tmpp = new place(i, height[i]);
qR.push(tmpp);
}
}
int rectR = ;
while (qR.size() > ) {
place* tmpp = qR.front();
qR.pop();
int len = tmpp->idx - qR.front()->idx;
rectR += tmpp->height * len;
}
place* heighestR = qR.front();
qR.pop();
int earthAmtR = ;
for (int i = (int)height.size()-; heighestR->idx < i ; i--) {
earthAmtR += height[i];
}
int waterR = rectR - earthAmtR;
int res = waterL + waterR;
return res;
}
[LeetCode] 42. Trapping Rain Water 解题思路的更多相关文章
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- [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的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- Java [Leetcode 42]Trapping Rain Water
题目描述: Given n non-negative integers representing an elevation map where the width of each bar is 1, ...
- [leetcode]42. 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(积水体积)
题目链接: https://leetcode.com/problems/trapping-rain-water/?tab=Description Problem: 根据所给数组的值,按照上图的示意 ...
随机推荐
- 常用命令su ls cp cd mv cat touch mkdir rm head less more pwd tac 等
1.用户切换 su:switch user su kevin //半切换,切换到kevin用户,但是不读取kevin用户的配置文件 su - kevin //完全切换,执行这个命令的时候表示切 ...
- jQuery选择器(适合初学者哟....)
选择器是jQuery最基础的东西,本文中列举的选择器基本上囊括了所有的jQuery选择器,也许各位通过这篇文章能够加深对jQuery选择器的理解,它们本身用法就非常简单,我更希望的是它能够提升个人编写 ...
- css3 标题超过长度自动省略号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- php分页笔记
在做留言板的时候,用到了分页,所以写了这个分页笔记 既然已经开始写分页了,肯定掌握了了php的一些知识以及mysql的基本操作 在做分页的时候,我也遇到了很多问题,但是大家不要怕,无论什么问题 ...
- FastCgi与PHP-fpm关系[转] 读完本文瞬间明朗了很多
刚开始对这个问题我也挺纠结的,看了<HTTP权威指南>后,感觉清晰了不少. 首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. ...
- validate插件的使用
方法如下: 插件: jquery.validate.js jquery.validate.custom.js bootstrap html代码: <form id="form_name ...
- 安装Oracle 11gR2,报错:[INS-06101] IP address of localhost could not be determined
安装Oracle 11gR2,报错:[INS-06101] IP address of localhost could not be determined 出现这种错误是因为主机名和/etc/host ...
- POJ 3273 Monthly Expense 二分枚举
题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...
- cocos2d-x Tests讲解 Particle System(粒子系统)
转载请注明出处: http://www.cnblogs.com/shangdahao/archive/2012/04/14/2447571.html 一.粒子系统简介: 粒子系统最早出现在80年代,主 ...
- Mybatis 学习
1. Mybatis 中 # 与 $ 符号的区别: a. #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号. 如:order by #user_id#,如果传入的值是12,那么解 ...