[LeetCode] 42. Trapping Rain Water_hard tag: Two Pointers
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.

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.
Example:
Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6 这个题目思路类似于木桶原理, 中间能收集到的水, 取决于左右两边较短的高度, 然后分别往中间走, 直到两者相遇. 所以设置l, r, lh(left height), rh(right height), 判断l,r指向的building
那个短就移动哪个, 比如l 指的更短, 那么l+= 1, 然后如果新building比相应的left height要短, ans += left height - building[l], 否则 height = building[l], 如果r指的更短, 那么
情况类似. 1. Constraints
1) empty, edge case, return 0
2) element will be >= 0 integer 2. Ideas Two points: T:O(n) S: O(1) 3. Code
class Solution:
def trapRainWater(self, heights):
if not heights: return 0
l, r, ans = 0, len(heights) -1, 0
lh, rh = heights[l], heights[r]
while l < r:
if heights[l] <= heights[r]:
l += 1
if heights[l] < lh:
ans += lh - heights[l]
else:
lh = heights[l]
else:
r -= 1
if heights[r] < rh:
ans += rh - heights[r]
else:
rh = heights[r]
return ans
4. Test cases
1) empty
2) 1, 2, 3
3)
[0,1,0,2,1,0,1,3]
[LeetCode] 42. Trapping Rain Water_hard tag: Two Pointers的更多相关文章
- [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 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
		
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
 - LeetCode - 42. Trapping Rain Water
		
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
 - [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(凹槽的雨水) 解题思路和方法
		
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 ...
 - [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: 根据所给数组的值,按照上图的示意 ...
 
随机推荐
- 跨域 - jsonp轻松搞定跨域请求
			
1.jsonp轻松搞定跨域请求 vue中使用axios,遇到跨域我就蒙逼了.第一次真正意义上的尝试使用jsonp js中用 var myscript = document.createElement( ...
 - About LOCAL_PRIVATE_PLATFORM_APIS in Android.mk
			
LOCAL_PRIVATE_PLATFORM_APIS := true设置后,会使用sdk的hide的api來编译 在Android.mk中如果有LOCAL_SDK_VERSION 这个编译配置,就会 ...
 - sencha touch 开发环境搭建(视频)
			
图文文章参见: http://www.cnblogs.com/mlzs/p/3420900.html 视频共享链接 百度:http://pan.baidu.com/s/1mg5DpS8
 - [工具] 护眼宝 – 傻瓜版屏幕蓝光过滤应用[Win/Android]
			
护眼宝 是一款 Windows.Android 下的屏幕蓝光过滤工具,傻瓜式操作,支持智能模式.疲劳提醒,可以有效的保护视力及减小夜间使用电脑.手机对睡眠对影响. 来自发现频道. 类似应用有很多了 ...
 - JPEG图片扩展信息读取与修改
			
extends:http://www.2cto.com/kf/201405/303813.html 读写均是键值对的方式,需要注意的是值的类型需要严格按照api定义格式. 支持读写节点为: 1.TAG ...
 - Laravel 查询包括软删除的记录
			
查询结果包括已被软删除的记录: Model::withTrashed()->get(); 只查询软删除记录: Model::onlyTrashed()->get(); PS:个人博客-La ...
 - Visual Studio启用64位 IIS Express 解决 x64位的dll 而出现 未能加载文件或程序集“xxxxxxxx”或它的某一个依赖项。试图加载格式不正确的程序。
 - ESXI虚拟机磁盘管理(精简-厚置-精简)
			
VMwareESX/ESXi 精简置备(thin)与厚置备(thick)虚拟机磁盘之间转换 VMwareESX/ESXi 虚拟机磁盘thin与thick之间转换 注意:转换前请先关闭虚拟机!!! 一. ...
 - C++ polymorphism Virtual Function  多态  虚函数
			
Polymorphism in C++ https://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm https://github.com ...
 - pandas绘图总结
			
转自:http://blog.csdn.net/genome_denovo/article/details/78322628 pandas绘图总结 pandas中的绘图函数(更加详细的绘图资料可参考p ...