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. oracle启动脚本 .

        .#!/bin/bash set -x su -oracle >>EON lsnrctl start sqlplus /nolog >>EOF conn / as sy ...

  2. 第九周PSP

     工作周期:11.10-11.17 本周PSP: C类型 C内容 S开始时间 ST结束时间 I中断时间 T净时间(分) 文档 写随笔(PSP) 19:00min 22:00min 30min 90mi ...

  3. lvs + keepalived 介绍及安装

    LVS介绍 lvs 核心ipvs      Ipvs(IP Virtual Server)是整个负载均衡的基础,如果没有这个基础,故障隔离与失败切换就毫无意义了.Ipvs 具体实现是由ipvsadm ...

  4. 随机生成数字(ashx文件,调用上篇所写发送邮件代码)

    public void ProcessRequest(HttpContext context) { //邮件标题 string Email_Title = Dsis.Core.SysCore.PubF ...

  5. INI配置文件分析小例子

    随手写个解析INI配置字符串的小例子 带测试 #include <iostream> #include <map> #include <string> #inclu ...

  6. BingMap

    Application name Key details BngMapTest Key:25nTPiuDe0kxITMR1ymE~j5IlskEImiwGsGmAnsCftQ~Ap0HigfJujLq ...

  7. racle wm_concat(column)函数的使用

    oracle wm_concat(column)函数使我们经常会使用到的,下面就教您如何使用oracle wm_concat(column)函数实现字段合并,如果您对oracle wm_concat( ...

  8. 怎样防止重复发送 Ajax 请求?

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:长天之云链接:http://www.zhihu.com/question/19805411/answer/15465427来源 ...

  9. css js 的引入方式和书写位置

    css 的引入方式 1.行内样式 <div id="div1" style="width:100px; height:100px; background:red&q ...

  10. pip 8 安装

    1.首先安装setuptools 2.再安装pip http://blog.csdn.net/u013372487/article/details/51726002