Trapping Rain Water (Bar Height) -- LeetCode
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的更多相关文章
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 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: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...
- [LeetCode] 42. 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 II 收集雨水之二
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
随机推荐
- Python 第一周编程作业
一. 编程题 1. 结合turtle库使用手册,读懂下列代码,并在jupyter编译器中运行观察结果: 依次分析下代码: 第一行 通过保留字import引用了Python中用于绘制图形的turtl ...
- HDU 2295 Radar (二分 + Dancing Links 重复覆盖模型 )
以下转自 这里 : 最小支配集问题:二分枚举最小距离,判断可行性.可行性即重复覆盖模型,DLX解之. A*的启发函数: 对当前矩阵来说,选择一个未被控制的列,很明显该列最少需要1个行来控制,所以ans ...
- EXTJS4.0 form 表单提交 后 回调函数 不响应的问题
在提交表单后,应返回一个 JSON 至少要包含{success:true} 否则,EXT 不知道是否成功,没有响应. {success:true,msg:'成功',Url:'http://www.ba ...
- hdu 2616 Kill the monster (DFS)
Kill the monster Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 少年Pi的奇幻漂流
选择怀疑作为生活哲学就像选择静止作为交通方式. 的确,我们遇见的人可能改变我们,有时候改变如此深刻,在那之后我们成了完全不同的人,甚至我们的名字都不一样了. 声音会消失,但伤害却留了下来,像小便蒸 ...
- 如何在CentOS7上改变网络接口名
如何在CentOS7上改变网络接口名 传统上,Linux的网络接口被枚举为eth[0123...],但这些名称并不一定符合实际的硬件插槽,PCI位置,USB接口数量等,这引入了一个不可预知的命名问题( ...
- task [最大权闭合子图]
题面 思路 其实仔细读透就发现,是一个最大权闭合子图的模型 套进网络流里面就挺好做的了 可以选择重载这道题里面的一些运算(加减,取最小值),这样比较方便 Code #include<iostre ...
- Guns V2.5
Guns V2.5 新版Guns基于SpringBoot全面升级,完美整合springmvc + shiro + MyBatis 通用 Mapper + 分页插件 PageHelper + beetl ...
- Sed替换文本中的指定内容
可以这样做:方法1:sed -i 's/被替换的内容/要替换成的内容/' file 方法2:sed 's/被替换的内容/要替换成的内容/g' file > file.outmv file.out ...
- 转:Java SoftReference 使用构建对象缓存
本文介绍对象的强.软.弱和虚引用的概念.应用及其在UML中的表示. 1.对象的强.软.弱和虚引用 在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说, ...