【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 ------------------------------------------------------------- ...
随机推荐
- windbg不识别pdb文件符号
一开始配置完毕后 输入reload 但不识别 输入reload -f 还是不识别 输入reload -f 模块名 继续不识别 !sym noisy 查看 输入reload 发现有了一堆的查找路径 把 ...
- 自定义UISearchBar
先上系统默认的UISearchBar,然后用KVO修改 UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:_topView.boun ...
- Ubuntu 14.04 server ssh 远程服务遇到的一点事儿
ubuntu server 14.04 root@ubuntu:/# lsb_release -aNo LSB modules are available.Distributor ID: Ubuntu ...
- iOS设置导航栏标题
方法一:在UIViewController中设置self.title. 方法二:设置self.navigationItem.titleView.
- 编写serversocket简单示例1
package j2se.core.net.tcp; import java.io.DataOutputStream;import java.io.IOException;import java.ne ...
- 基类用的this指针
结论:基类构造函数中的this指针指向的是派生类的对象 测试代码: #include <iostream> using namespace std; class father; fathe ...
- VBA使用的Randomize和DoEvents
Randomize private function getInt() dim n,m as integer Randomize n=1 m=3 getInt=Int((m+1-n)*rnd + n) ...
- mysql主从切换
mysql 主从切换 主停,从做主步骤如下: 1 确认从服务器已经完成所有同步操作:stop slave io_thread show processlist 直到看到状态都为:xxx has rea ...
- 《Linux多线程服务端编程:使用muduo C++网络库》上市半年重印两次,总印数达到了9000册
<Linux多线程服务端编程:使用muduo C++网络库>这本书自今年一月上市以来,半年之内已经重印两次(加上首印,一共是三次印刷),总印数达到了9000册,这在技术书里已经算是相当不错 ...
- 【温故Delphi】双击工程文件打开软件
问题描述 大部分软件都有鼠标双击文件,就能打开所关联的软件并打开所选工程,这是如何做到的呢? 把文件关联到一个程序中,双击文件来启动程序,那么这个文件名称就是这个程序的命令行的一个参数. 所以要想实现 ...