[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: 根据所给数组的值,按照上图的示意 ...
随机推荐
- 如何在Ubuntu上创建及管理LXC容器?
将LXC安装到Ubuntu上 $ sudo apt-get install lxc 安装完毕之后,运行lxc-checkconifg工具,检查当前Linux内核支持LXC的情况.要是一切都已被启用,内 ...
- absolute之实现居中的三种方式
居中思想可以由很多方式实现,这篇文章采用absolute实现:由传统方式开始到absolute独立使用方式 方式一:传统方式,父容器relateive,子元素absolute,然后left:50%,再 ...
- 初识pngdrive
初识是第一次认识的意思,类似的词还有初见.初遇.初心.初愿.初恋.初吻……梦里相见如初识,很美好的感觉.同样,今天我们要认识的也是一个比较神奇美妙的东西,至少对于程序员来说. 我曾经尝试过很多文件加密 ...
- HTML5学习笔记----html5与传统html区别
一. HTML5语法的改变 该知识点所说变化指的是基于HTML4基础上所定义的改变,主要有如下: HTML5的文件扩展符(.html或.htm)与内容类型(text/html)保持不变. HTML5中 ...
- js 中对象属性特性2
对象的存储描述: get 和 set 方法 <script> var obj ={ get age(){ return 22 }, set age(value){ console. ...
- IE6下解决select层级高的问题
div在IE6下无法遮盖select,原因是在IE6下,浏览器将select元素视为窗口级元素,这时div或者其它的普通元素无论z-index设置的多高都是无法遮住select元素的. 解决方法有三种 ...
- 【python】python的列表表达式或解析式,帅就一个字
>>> list1 = [x**2 for x in range(10)]>>> list1[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
- Java 单链表的倒置
在面试,笔试的过程中经常会遇到面试官问这种问题,实现单链表的倒置方法.现在对单链表的倒置犯法做个记录,方便自己以后查看. 单链表的定义: public class Node { int v; Node ...
- How to get Directory size in IsolatedStorage of Windows Phone 8 App
There is no API to get the total size of a specific directory in the isolated storage. Therefore, th ...
- UI控件切圆角
1. xib下设置View圆角 这个很简单, 只需要重写 - (void)drawRect:(CGRect)rect 方法就行了 1 2 3 4 5 6 - (void)drawRect:(CGRe ...