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.

The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!

思路:使用两个指针left和right,分别从两端向中间靠拢。同时,记录left和right曾经到过的最高值。如果当前值并没有超过最高值,则将最高值与当前的差值作为水的量,并将指针加一(right指针是减一)。为了让这个方法有效(无效的情况是,指针后续遇见的bar都不会再高于最高值了,那么这些水根本存不起来),因此我们每次只移动left和right中矮的那一个(如果两者相等,则移动left)。这样,一定能保证最后会碰到比最高值要高的bar(至少是等高),因为我们移动的是矮的那个指针,往前移动肯定最后会遇见另一个高的指针。

 class Solution {
public:
int trap(vector<int>& height) {
if (height.size() == ) return ;
int left = , right = height.size() - , res = ;
int maxleft = height[left], maxright = height[right];
while (left < right)
{
if (height[left] <= height[right])
{
if (height[left] > maxleft) maxleft = height[left];
else res += maxleft - height[left];
left++;
}
else
{
if (height[right] > maxright) maxright = height[right];
else res += maxright - height[right];
right--;
}
}
return res;
}
};

bar height问题:这是Amazon面试中的一道问题。求最高的液面高度。这里只需要把上方的代码修改一下就可以用。就是在res值变更时,若max与当前高度差值非零时,记录下max值,最后最高的max值就是结果。

 class Solution {
public:
int trap(vector<int>& height) {
if (height.size() == ) return ;
int left = , right = height.size() - , res = ;
int maxleft = height[left], maxright = height[right];
while (left < right)
{
if (height[left] <= height[right])
{
if (height[left] > maxleft) maxleft = height[left];
else if (maxleft - height[left] > )
res = max(res, maxleft);
left++;
}
else
{
if (height[right] > maxright) maxright = height[right];
else if (maxright - height[right] > )
res = max(res, maxright);
right--;
}
}
return res;
}
};

Trapping Rain Water (Bar Height) -- LeetCode的更多相关文章

  1. [Leetcode][Python]42: Trapping Rain Water

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...

  2. [LeetCode] Trapping Rain Water 收集雨水

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

  3. leetcode#42 Trapping rain water的五种解法详解

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

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

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

  5. LeetCode: Trapping Rain Water 解题报告

    https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...

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

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

  7. [LeetCode] Trapping Rain Water II 收集雨水之二

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

  8. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  9. leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

随机推荐

  1. day06_08 字符串

    1.0 双引号和单引号的区别 a = "Let's go" print(a) #>>>Let's go 2.0 重复输出字符串* print('hello'*2) ...

  2. matlab使用摄像头人脸识别

    #关于matlab如何读取图片.视频.摄像头设备数据# 参见:http://blog.csdn.net/u010177286/article/details/45646173 但是,关于摄像头读取,上 ...

  3. ES原理(转载)

    该博客属于转载,是很经典的一篇关于ES的介绍: Elasticsearch 是一个兼有搜索引擎和NoSQL数据库功能的开源系统,基于Java/Lucene构建,可以用于全文搜索,结构化搜索以及近实时分 ...

  4. CodeForces Round #515 Div.3 C. Books Queries

    http://codeforces.com/contest/1066/problem/C You have got a shelf and want to put some books on it. ...

  5. 发现一个form小问题

    在使用编辑器及框架时,form表单如果在太靠内的div层里,就取不到textarea的post值,具体原因位置,可能跟框架的CSS有关

  6. 有许多部分没有在cgroup中显示啊,current/high/low/min等等

    没看见current/high/low/min 在cgroup中的显示内容

  7. RocketMQ 源码分析 RouteInfoManager(四)

    在上一章分析了NamesrvController的构造函数时,会生成一个RouteInfoManager对象,该对象存放着整个消息集群的相关消息,所以这里单独拿出来分析.其实试想一下namesrv的功 ...

  8. BZOJ 1208 [HNOI2004]宠物收养所 | SPlay模板题

    题目: 洛谷也能评 题解: 记录一下当前树维护是宠物还是人,用Splay维护插入和删除. 对于任何一次询问操作都求一下value的前驱和后继(这里前驱和后继是可以和value相等的),比较哪个差值绝对 ...

  9. code forces 994B

    B. Knights of a Polygonal Table time limit per test 1 second memory limit per test 256 megabytes inp ...

  10. 汕头市队赛 SRM13 T2

    这道题很容易想到是二分 但是因为可能会爆LL 所以要加一波特判 #include<cstdio> #include<cstring> #include<algorithm ...