leetcode problem 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!
思路:
这一道题类似于leetcode 11 题Container With Most Water. 要求最多能装多少水.有两种思路.
思路一:
先从左往右遍历.每遍历一个数字,就贪心得到这个数字所在的格子能最多装水. 由于是从左到右遍历,我们只考虑的左边的高度,没有考虑右边的高度(实际应该选择两者小的那个). 因此需要再从右往左遍历,同样贪心这个数字所在格子
最多能装多少水,这次以右边高度为准. 最终综合从左到右和从右到左的结果,取两者小的.
代码:(runtime 19ms)
class Solution {
public:
int trap(vector<int>& height) {
vector<int> leftToRight;
vector<int> rightToLeft;
aux_function(height.begin(), height.end(), leftToRight);
aux_function(height.rbegin(), height.rend(), rightToLeft);
int ans = ;
auto it1 = leftToRight.begin();
auto it2 = rightToLeft.rbegin();
while (it1 != leftToRight.end()){
ans += min(*it1++, *it2++);
}
return ans;
}
private:
template<class Iter>
void aux_function(Iter begin, Iter end, vector<int> &ret) {
int left = ;
for (auto it = begin; it != end; it++) {
left = left > *it ? left : *it;
ret.push_back(left - *it);
}
}
};
思路二:

如上图中所示, 先求黑色格子与蓝色格子的总面积,然后再减去黑色各自面积.
代码:(runtime 10ms)
class Solution {
public:
int trap(vector<int> A) {
int n = A.size();
int summap = ;
int sumtot = ;
for(int i = ; i < n; i++) summap += A[i];
int left = , right = n - ;
int leftbar = , rightbar = ;
while(left <= right) {
leftbar = max(A[left], leftbar);
rightbar = max(A[right], rightbar);
if(leftbar <= rightbar) {
sumtot += leftbar;
left++;
//right--;
} else {
sumtot += rightbar;
right--;
//left++;
}
}
return sumtot - summap;
}
};
leetcode problem 42 -- Trapping Rain Water的更多相关文章
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- 【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
一天一道LeetCode系列 (一)题目 Given n non-negative integers representing an elevation map where the width of ...
- 【LeetCode】42. Trapping Rain Water 接雨水 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...
- LeetCode OJ 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
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- 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 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
随机推荐
- nape.geom.MarchingSquares
Nape中的MarchingSquares类很简单,只有一个静态函数run,不过这对绘制那些简单的形状来说,已经足够了(当然MarchingSquares能做的不只这些).下面是这个run方法的结构: ...
- VS2012的SVN插件VISUALSVN
http://www.visualsvn.com/visualsvn/download/
- 简单详细的OD破解教程
2007-08-04 15:46作者:CCDebuger注:昨天在网上见到了这篇文章,但缺少插图,从另外一篇文章中也看到了类似的的教程文章,里面的插图质量实在不敢恭维.在一个论坛中正好下载了文章中所介 ...
- linux就是这个范儿之融于心而表于行(1)
原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处 .作者信息和本声明.否则将追究法律责 时间总是过得那么快,如流水一般哗啦啦的就淌走了一大堆!周遭事事沧桑变迁喧哗或耳语中流传的故事已渐模糊 ...
- 数据库升级ora-04063 DBMS_REGISTRY has error
在做Oracle数据库从11.2.0.1.0升级到11.2.0.2.8时,软件升级没有问题,实例升级没有问题,升级psu的时候. 执行@?/rdbms/admin/catbundle psu appl ...
- TQ210裸机编程(2)——LED流水灯
两个文件start.S和led.c start.S .global _start @声明一个全局的标号 _start: bl main ...
- android 基站定位
package cn.LocationStation; import java.io.BufferedReader; import java.io.InputStream; import java.i ...
- G711
G.711就是语音模拟信号的一种非线性量化.细分有二种:G.711 a-lawand G.711 u-law.不同的国家和地方都会选取一种作为自己的标准. G.711a/u bitrate 是64kb ...
- 记 Ubuntu14.04 Monodevelop 安装的两个问题
1. Monodevelop 不能执行,显示错误 The assembly mscorlib.dll was not found or could not be loaded. 首先要确定mono安装 ...
- 文件I/O(不带缓冲)之creat函数
本篇博文内容摘自<UNIX环境高级编程>(第二版),仅作个人学习记录所用.关于本书可参考:http://www.apuebook.com/. 也可调用creat函数创建一个新文件. #in ...