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 解题思路的更多相关文章

  1. LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))

    LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...

  2. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

  3. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

  4. LeetCode - 42. Trapping Rain Water

    42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...

  5. leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  6. [LeetCode] 42. Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  7. Java [Leetcode 42]Trapping Rain Water

    题目描述: Given n non-negative integers representing an elevation map where the width of each bar is 1, ...

  8. [leetcode]42. Trapping Rain Water雨水积水问题

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  9. LeetCode 42 Trapping Rain Water(积水体积)

    题目链接: https://leetcode.com/problems/trapping-rain-water/?tab=Description   Problem: 根据所给数组的值,按照上图的示意 ...

随机推荐

  1. C语言库函数--操作文件

    //C库函数读取文件的代码 I/O缓冲机制 C语言库函数写文件都是写在内存中,然后一次写入磁盘.提高了效率. 读写文件,不对系统进行操作,一般采用C语言库函数.移植可以在任何可以对C支持的操作系统,而 ...

  2. .NET多线程编程(转)

    在.NET多线程编程这个系列我们讲一起来探讨多线程编程的各个方面.首先我将在本篇文章的开始向大家介绍多线程的有关概念以及多线程编程的基础知识;在接下来的文章中,我将逐一讲述.NET平台上多线程编程的知 ...

  3. 【转】JSONP简介

     原文链接:说说JSON和JSONP,也许你会豁然开朗,含jQuery用例 先说说JSONP是怎么产生的: 1.一个众所周知的问题,Ajax直接请求普通文件存在跨域无权限访问的问题,甭管你是静态页面. ...

  4. IDEA插件开发基础

    由于简易ORM的需要,想要做一些代码自动生成功能(通过右键菜单辅助) 半自动编写代码,故考虑需要开发IDE插件(我司现使用IDEA) 1.例子代码http://confluence.jetbrains ...

  5. corosync+pacemaker and drbd实现mysql高可用集群

    DRBD:Distributed Replicated Block Device 分布式复制块设备,原理图如下 DRBD 有主双架构和双主架构的,当处于主从架构时,这个设备一定只有一个节点是可以读写的 ...

  6. C语言中字符型和字符串型的区别?

    C语言中只有字符型类型,没有字符串型类型.字符类型用一个带符号的8位二进制编码表示,其性质与int相同,只是只有一个字节.表示字符的ASCII编码使用其中的0~127,所以要明白字符类型(char)其 ...

  7. [转]ef获取某个表中的部分字段值

    我有个新闻表 id,title,body,createtime,author,click 使用ef4.1 仅仅读取 id,title,createtime 并显示在页面上. public static ...

  8. MAC安装XAMPP的出现无法打开Apache server

    安装MAMP后,启动服务时提示Apache启动失败,80端口被占用.查看进程发现存在几个httpd. OS X自带Apache,可是默认是没有启动的.我也没有开启Web共享,怎么就开机启动了呢? 不知 ...

  9. how to develop mobile web

    http://blog.templatemonster.com/2010/05/11/how-make-mobile-website-6-easy-tips/ http://mobile.smashi ...

  10. 为什么 API 监控对于任何业务来说都重要?

    对于商务运算来说一个比较稳定的趋势在于对 API 日渐增长的依赖性,几乎每一个代码级交互过程都会调用 API 来收集数据或触发某些关键过程.没有 API ,你将无法与同伴进行文件交流,没有 API , ...