【leetcode】Trapping Rain Water
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的更多相关文章
- 【题解】【直方图】【Leetcode】Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 【leetcode】Trapping Rain Water(hard)
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- [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 ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [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 ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
随机推荐
- C# 文件操作 把文件读取到字节数组
string zipfile = "c:\\a.zip"; //方法1 FileStream fs = new FileStream(zipfile, FileMode.Open) ...
- configparser配置文件操作
configparser 模块用于对配置操作 官方文档地址https://docs.python.org/3/library/configparser.html 导入configparser模块 i ...
- 终端 git log 修改样式
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d ...
- linux环境下jdk的安装步骤
JDK的安装步骤:1. 把jdk文件cp到服务器上2. 加权限 chmod +x 文件3. 执行 ./4.修改配置 vi /etc/profile 最后一行添加:export JAVA_HOME=/ ...
- CMake 使用方法(转)
CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程).他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的 ...
- caffe net 可视化工具
http://ethereon.github.io/netscope/#/editor 将对应的网络输入到里面,然后按shift+enter即可查看对应的网络结构
- 在Xcode中使用Git进行源码版本控制
http://www.cocoachina.com/ios/20140524/8536.html 资讯 论坛 代码 工具 招聘 CVP 外快 博客new 登录| 注册 iOS开发 Swift Ap ...
- angularjs 作用域
1.指令属性取值:通过attr.someAttribute属性名字获取 以下,通过$eval(attr.data)获取value <div ng-controller="personC ...
- CAD 二次开发--属性块
1.属性块的定义 属性块是有构成的实体和附加信息(属性)组成的,属性块中块的定义与简单块中块的定义一样,而属性的定义主要是通过属性的AttributeDefinition类的有关属性和函数来实现的.具 ...
- text-align:justify的使用
在平常的开发过程中,对于text-align一般用到的是left,center,right,这三个属性都不会陌生.然而,对于justify的使用我却是很陌生.首先有个比较简单的例子. 首先是html代 ...