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的更多相关文章

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

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

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

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

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

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

  4. LeetCode - 42. Trapping Rain Water

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

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

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

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

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

  7. [LeetCode] 42. Trapping Rain Water 解题思路

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

  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. Undeclared identifier:XXX

    未识别错误,是因拼写错误或找不到定义文件. 下面列举一些类型和函数用到的单元. _Stream ADODB_TLB akTop, akLeft, akRight, akBottom Controls ...

  2. 【转】java:Session(会话)机制详解

    书中讲:以下情况,Session结束生命周期,Servlet容器将Session所占资源释放:1.客户端关闭浏览器2.Session过期3.服务器端调用了HttpSession的invalidate( ...

  3. Makefile eval函数

    https://www.cnblogs.com/gaojian/archive/2012/10/04/2711494.html对 makefile 中 eval 函数的学习体会 http://blog ...

  4. 开发常见错误之 : IMP-00058: 遇到 ORACLE 错误 1691

    IMP-00058: 遇到 Oracle 错误 1691ORA-01691: Lob 段YQPRO.SYS_LOB0000031467C00006$$无法通过128(在表空间YQPRO中)扩展这种情况 ...

  5. 题目1198:a+b(高精度计算,好像有点问题)

    题目链接:http://ac.jobdu.com/problem.php?pid=1198 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  6. LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)

    题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description   Problem: 在已知递减排序的数组中,查找到给定 ...

  7. [NOI2005]月下柠檬树[计算几何(simpson)]

    1502: [NOI2005]月下柠檬树 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1169  Solved: 626[Submit][Status] ...

  8. matlab中画系统零极点的方法

    写论文的时候由于需要画出系统的零极点图.但是之前不知道怎么用matlab画,今天研究了一下,拿出来和大家共享.所用到的matlab函数为zplane,matlab给出的解释如下: ZPLANE Z-p ...

  9. 【C#】简单计算器源代码

    form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.D ...

  10. Nginx 安装 --编译模块参数

    公司空出来一些服务器,很久没有来练手了,于是便开始有了这篇博客,记录下过程. Nginx 这个不多说了,名声在外,人们喜爱使用这款软件,主要还是因为它的高并发特性,公司也在用效果还不错,也用了它的一些 ...