[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: 根据所给数组的值,按照上图的示意 ...
随机推荐
- Oracle 10g 下载地址
Oracle Database 10g Release 2 (10.2.0.1.0) Enterprise/Standard Edition for Microsoft Windows (32-bit ...
- 简单Linq笔记
Linq是.net 3.5才引入的 要引入命名空间System.Linq. Linq to XML要引入System.Xml.Linq Linq to ADO.NET要引入System.Data.L ...
- 【转】JSONP简介
原文链接:说说JSON和JSONP,也许你会豁然开朗,含jQuery用例 先说说JSONP是怎么产生的: 1.一个众所周知的问题,Ajax直接请求普通文件存在跨域无权限访问的问题,甭管你是静态页面. ...
- mui h5 动态实现数据的移除和数据操作完后的重新获取
HTML 代码 <ul class="mui-table-view" id="OA_task_1"> <li class="mui- ...
- 青瓷qici - H5小游戏 抽奖机 0 创建工程
安装运行平台需要nodejs,具体方法请参照官方说明文档. 运行后打开了一个空空的窗口. 首先我们进行工程设置,菜单>工程>设置 菜单里面设置我们游戏的名称,到时候会显示在游戏的title ...
- HTML5格式化
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- AndroidStudio Gradle版本不匹配问题
报错信息: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } Error:(1, 1) A problem occurr ...
- PBS
赞同,已经试验成功.后来查手册: $man qdel-p 的功能是强制净化队列.这个 "p" 可能是 "purge" 的缩略形式 qsub,qdel,qmgr ...
- 定位 - CoreLocation - 指南针
#import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewCont ...
- HTML5之本地文件系统API - File System API
HTML5之本地文件系统API - File System API 新的HTML5标准给我们带来了大量的新特性和惊喜,例如,画图的画布Canvas,多媒体的audio和video等等.除了上面我们提到 ...