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. Disk Genius 彻底清理硬盘空闲

  2. c++ 类内static成员初始化

    类内部的static成员,除了为const static 且为整数类型(int char bool)可在类内部初始化. 其他的都建议在对应的cpp文件中进行初始化. test.h #ifndef TE ...

  3. 【大数据系列】基于MapReduce的数据处理 SequenceFile序列化文件

    为键值对提供持久的数据结构 1.txt纯文本格式,若干行记录 2.SequenceFile key-value格式,若干行记录,类似于map 3.编写写入和读取的文件 package com.slp; ...

  4. enum hack

    关于占用内存的大小,enum类型本身是不占内存的,编译器直接替换.但是enum类型的变量肯定是占内存的. class A{ public: //enum类型本身不占内存 enumEnumTest{ a ...

  5. Ubuntu 14.04 LTS 火狐浏览器中,鼠标选择文字被删除的解决办法

    这篇文章主要介绍了Ubuntu 火狐浏览器中,鼠标选择文字被删除的解决办法,需要的朋友可以参考下在终端中输入命令: ibus-setup将 “在应用程序窗口中启用内嵌编辑模式“ 选项取消

  6. slave库写redo、binlog不实时丢数据的场景

    1.slave涉及相关文件 slave读取master的binlog日志后,需要落地3个文件:relay log.relay log info.master info: relay log: 即读取过 ...

  7. Clojure学习之比线性箭头操作

    1. 单箭头( -> ) 单箭头操作符会把其参数form迭代式地依次插入到相邻的下个一个form中作为该form的第一个参数.这就好像把这些form串起来了,即线性化(Threading). 由 ...

  8. sencha touch list 选择插件,可记忆已选项,可分组全选

    选择记忆功能参考:https://market.sencha.com/extensions/ext-plugin-rememberselection 插件代码: /* * 记住列表选择状态 * 如果分 ...

  9. [Offer收割]编程练习赛15 A.偶像的条件[贪心]

    #1514 : 偶像的条件 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi的学校正面临着废校的大危机.面对学校的危机,小Hi同学们决定从ABC三个班中各挑出一名同 ...

  10. 总结一下最近用到的技术(1)--ultraESB

    最近项目中方法用到了一些新的东西,由于之前没有用到过,现在总结一下,方便以后查阅,本篇文章介绍ultraESB,接下来的文章会介绍JsonSchema,JsonSchamaValidator,Json ...