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. C# 动软生成器对应的Access数据库操作类DbHelperOleDb

    using System;using System.Collections;using System.Collections.Specialized;using System.Data;using S ...

  2. 中兴MF667S WCDMA猫Linux拨号笔记

    公司最近有个国外有个项目需要用到WCDMA猫,网上简单选型了一下决定使用ZTE的型号MF667S的猫,本以为在Linux下拨号是比较简单的(之前有两款3G猫的调试经验),估计半天能搞定,结果折腾了一周 ...

  3. 用c和c++的方式实现栈

    #include <stdio.h> #include <stdlib.h> #include <assert.h> struct LinkNode { int d ...

  4. ubuntu下安装wordpress

    网上大多都是说放在var/www下面 实际上新版的ubuntu默认放在 var/www/html 下面 当然这个配置是可以修改的

  5. MMS源码中异步处理简析

    1,信息数据的查询,删除使用AsycnQueryHandler处理 AsycnQueryHandler继承了Handler public abstract class AsyncQueryHandle ...

  6. 【转】Nginx服务器详细配置含注释

    #使用的用户和组 user www www; #指定工作衍生进程数(一般等于CPU的总核数或总核数的两倍) worker_processes 8; #指定错误日志存放的路径,错误日志的记录级别可为de ...

  7. 收藏的 500多个开源的Git源码

    由dkhamsing发起的Open-Source iOS Apps收集了各种开源的iOS App,并进行了详细的分类,比如游戏.社交.健康.键盘.定位.多媒体.新闻.办公.安全以及小工具类等.截至目前 ...

  8. python第二天基础1-1

    一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. if 1==1: name = 'wupeiqi' print name 二.三元运算 result = 值1  ...

  9. Android自定义View的实现方法,带你一步步深入了解View(四)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17357967 不知不觉中,带你一步步深入了解View系列的文章已经写到第四篇了,回 ...

  10. as画柱型图的简单算法(关于柱型图宽和间距问题)

    做统计数据,经常用到如下柱型图: 柱图的X轴宽度(W)是已知的,在不影响柱的美观度情况下,怎么确定柱的宽度(w1)和柱间距(p1)的具体数值或比例呢? 在X轴宽度(W)已确定,柱的个数(A)是个不定值 ...