[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: 根据所给数组的值,按照上图的示意 ...
随机推荐
- JWNL的配置使用
JWNL是什么? JWNL is an API for accessing WordNet-style relational dictionaries. It also provides functi ...
- Qt编写的RTSP播放器+视频监控(ffmpeg版本)
记得四年前就写了个简易版本的,当时写得非常粗糙,代码实在惨不忍睹,时隔多年后,重新写了个版本,同时还解决了以前不支持6画面8画面切换等异形布局的问题.1:可以用过目录下的rtsp.txt设置要显示的视 ...
- Android JNI与多线程
Java通过JNI调用本地C++代码是在同一个线程中的同步调用. JNI中如果新建的线程调用java的代码,那么java代码是运行在JNI线程中的:但是,如果调用UI相关的代码时需要与java主线程通 ...
- ftp主动与被动模式详解
FTP是仅基于TCP的服务,不支持UDP.与众不同的是FTP使用2个端口,一个数据端口和一个命令端口(也可叫做控制端口).通常来说这两个端口是21(命令端口)和20(数据端口).但FTP工作方式的不同 ...
- java的前缀自增自减和后缀自增自减
2.前缀自增自减法(++a,--a): 先进行自增或者自减运算,再进行表达式运算. 3.后缀自增自减法(a++,a--): 先进行表达式运算,再进行自增或者自减运算 实例: 实例 public cla ...
- mysql union查询
1.mysql总是通过创建并填充临时表来执行union查询; 2.除非要服务器消除重复的行,否则一定要用union all.如果没有all关键字,mysql会在临时表加个distinct选项,会导致临 ...
- 最简单,最实用的数据库CHM文档生成工具——DBCHM
DBCHM支持SqlServer/MySql/Oracle/PostgreSQL等数据库的表列批注维护管理. DBCHM有以下几个功能 表,列的批注可以编辑保存到数据库. 表,列的批注支持通过pdm文 ...
- 在 CSS 中,width 和 height 指的是内容区域的宽度和高度
增加内边距.边框和外边距不会影响内容区域的尺寸,但是会增加元素框的总尺寸. 改变宽度就可以,去掉
- hue安装及基本测试-笔记
#################################################################################################### ...
- android 控制POS机图文打印(二)
上一篇文章结束了ESC/POS的指令集,没看过的可以去看一下,可以当作工具文档来使用的 android 控制POS机图文打印(一) 这一篇正式介绍如何使用POS机来打印图文信息. 首先介绍一下,ESC ...