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.

我做题,一般是按照AC率排序选着做的,这道题一不留神,难度居然还是Hard,好吧,确实也难到了我。AC率居然还蛮高,我也是提交了3次之后才AC的。先说说我前两次的错误思路:第一次,我是对每一个点,往两边找最长升序,然后以左右两边的这两个序列的和作为一个坑,用来填雨水。但是针对[5,1,2,1,2,1,2,5]这样的序列,即在区间内部的升降,其实并不影响其盛水,故WA!第二次个人认为也是能完成的,但是需要考虑的情况比较多,比较烦人,不是最合适的方法,方法是从左到右扫描,扫到第一个比当前高的,作为一个区间,接着从区间末开始继续往后扫,以此类推,这方法能解决上面的问题,但是最后一个区间需要单独考虑。

其实是可以这样解决的:先找到最高的那个问题maxhigh,然后分别从两边往这个节点遍历,设置一个次高点temphigh,

如果当前节点比次高点高,则更新次高点,继续;

否则,用次高点减去当前节点,就是我们能盛水的体积。

就是这么简单粗暴,唯一的缺陷就是需要两次扫描,有强迫症的或者追求完美主意的,可以查看下网上的一个方法,是直接从两边开始扫描的,一次搞定。个人感觉代码看上去不够直观。

下面贴一下我自己的代码:

class Solution:
# @param A, a list of integers
# @return an integer
def trap(self, A):
res = 0
if len(A) < 3:
return 0
maxv = 0
maxid = 0
for i in range(len(A)):
if A[i] > maxv:
maxv = A[i]
maxid = i
temphigh = 0
for j in range(maxid):
if A[j] > temphigh:
temphigh = A[j]
else:
res += temphigh - A[j]
temphigh = 0
for j in range(len(A)-1,maxid,-1):
if A[j] > temphigh:
temphigh = A[j]
else:
res += temphigh - A[j]
return res

  

【leetcode】Trapping Rain Water的更多相关文章

  1. 【题解】【直方图】【Leetcode】Trapping Rain Water

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

  2. 【leetcode】Trapping Rain Water(hard)

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

  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的五种解法详解

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

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

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

  6. [LeetCode] 407. Trapping Rain Water II 收集雨水 II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  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] 407. Trapping Rain Water II 收集雨水之二

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  9. LeetCode - 42. Trapping Rain Water

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

随机推荐

  1. Netty 的 inbound 与 outbound, 以及 InboundHandler 的 channelInactive 与 OutboundHandler 的 close

    先看一个例子. 有一个简单 Server public class SimpleServer { public static void main(String[] args) throws Excep ...

  2. 第二章 jQuery数组和字符串

    章节内容: 1.利用数组在列表中显示名字 (1)利用数组显示名字列表--join()方法 (2)从数组中获取名字并追加到有序列表--each()方法 (3)利用HTML元素创建数组和计算数组长度--g ...

  3. JDK、JRE、JVM三者间的关系

    JDK(Java Development Kit)是针对Java开发员的产品,是整个Java的核心,包括了Java运行环境JRE.Java工具和Java基础类库.Java Runtime Enviro ...

  4. Co-saliency-Huazhu Fu

    这里主要是fu老师的显著性检测分割的一些资料. 对应的主页为:http://hzfu.github.io/ 对应的一些codes:https://github.com/HzFu

  5. 转载:分布式系统的CAP理论

    原文转载Hollis原创文章:http://www.hollischuang.com/archives/666 2000年7月,加州大学伯克利分校的Eric Brewer教授在ACM PODC会议上提 ...

  6. 03-Swift常量&变量

    什么是常量和变量 在Swift中规定:在定义一个标识符时必须明确说明该标识符是一个常量还是变量 使用let来定义常量,定义之后不可以修改 使用var来定义变量,定义之后可以修改 常量和变量的基本使用 ...

  7. ServiceStack.OrmLite中的一些"陷阱"(3)

    前文说到如果使用多数据库(不同SQL方言)时要如何开发?其实前文(第二篇)也有“透露”到.就是直接使用库提供的OrmLiteConnection 及OrmLiteConnectionFactory(I ...

  8. (python) 标准模块sys和os的使用

    一.sys模块 包含了系统的相关的功能.我们来学习sys.argv,它包含命令行参数. 例子:定义了一个add函数,用来实现两个整数的相加. #! coding=utf-8 # usersys.py ...

  9. IE全屏浏览代码

    以前做过一个网络版的商场导购触摸屏系统,用ASP写的,就是要在运行的时候全屏浏览而不能出现标题栏.工具栏.状态栏等.解决方法是用JS弹出全屏窗口,建立html文件,代码如下: <script l ...

  10. <转>C++11标准后的C++阅读书目

    C++11标准后的C++阅读书目 C++ 新标准 C++11 的发布导致了大批新书的出现,还有一些经典书籍也进行了更新.Andrew Binstock 在 Dr.dobbs 上给大家推介一些学习 C+ ...