[Leetcode] Container With Most Water ( C++)
题目:
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container.
Tag:
Array; Two Pointers
体会:
1. 这道题我觉得是双指针的一个创新用法。之前的双指针都是一主一辅,辅助的那个不断去探测下一个,然后主要的那个会根据探测结果做出相应动作,比如 Merge Sorted Array,这道题的双指针两个人是同等重要性,根据其他的判定条件来决定下一次移动谁。
2. 这道题之所以是双指针是同等位置,也可以从另外一个角度来感受,就是要知道左边那条线“和”右边那条线的位置,两个都是未知的,都是要确定的。
3. 回到题目上,O(N)的解法。代码很简单,可是能想到不容易。(我也是炒的别人的思路)。为什么每次是那样移动指针呢?假设h[left] < h[right],我们管当前用来围城面积的左边的这条竖着的直线叫Line left, 管右边的那条线要Line right。那么当前的面积就是 (area = (right - left) * h[left])。好了,我们假设还有一个更大的面积,下面我们要说明这个更大的面积一定不会是和Line left围成的。
假设这个更大的面积的其中一条边是Line x, (Line x肯定是位于Line left 和Line right之间啦)。那么Line和max围成的面积是( ( x - left) * h[left]) , 这个面积肯定是小于原来的 (right - left ) * h[left]的啦。
综上,我们肯定是要移动Line left了。
4. 可以从一个更数学的角度来解释这个问题:
假设left, right 是line left和line right在x轴上的坐标,起始时, left = 0, right = 最右边。那么
area = ( right - left ) * min (h[left], h[right])
怎么样才能使这个函数有更大的值呢?不管移动左边的线还是右边的线,结果都是(right - left)的值会变小,若要area的值变大,只能是把min(h[left], h[right])的值变大才有可能。所以如果我们继续保留较矮的那条线的话,这个min值一定不会变大,所以面积肯定不会变大;而保留原本较高的那条线,是有可能使min值变大的,所以只能是保留这条线。
class Solution {
public:
int maxArea(vector<int> &height) {
int left = ;
int right = height.size() - ;
int result = ;
int area = ;
while (left < right) {
if (height[left] < height[right]) {
area = (right - left) * height[left++];
} else {
area = (right - left) * height[right--];
}
if (area > result) {
result = area;
}
}
return result;
}
};
[Leetcode] Container With Most Water ( C++)的更多相关文章
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- [LeetCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode]Container With Most Water, 解题报告
前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n no ...
- C++LeetCode:: Container With Most Water
本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做.感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过.每天都要被LeetCo ...
- leetcode Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode] Container With Most Water 简要分析
前言 这题非要说贪心的话也算是吧,不过最主要的特征还是双指针.LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的.这道题倒是“短板效应”的不错体现了. 题目 题目链接 Given n non-neg ...
- LeetCode——Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- LeetCode Container With Most Water (Two Pointers)
题意 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
- [Leetcode] container with most water 最大水容器
Given n non-negative integers a1 , a2 , ..., an , where each represents a point at coordinate (i, ai ...
随机推荐
- C#设置鼠标在控件上面时,改变光标形状
//设置鼠标在控件上面时,改变光标形状 private void pictureBox_macroLogo_MouseHover(object sender, System.EventArgs e) ...
- CSSOM视图模式
相关技术文章: CSSOM视图模式(CSSOM View Module)相关整理 W3C CSSOM View Module
- jquery 1.9 之后toggle不能用的问题
今天用到toggle这个方法,发现不是自己想要的效果,之前有用过好多次,一直都没有问题. 网上查了原因,才知道是版本的问题,jquery1.9之后toggle取消了.那么如果想要继续用toggle的这 ...
- 关闭并且禁用ECSHOP缓存
ECSHOP的缓存机制从一定程度上可以减少ECSHOP反复读取数据库的几率,从而一定程度上降低服务器负担,提高访问速度.但是启用缓存机制,对一些新手站长也有不利的地方.我就遇到很多新手站长经常问,我明 ...
- Python一路走来 面向对象1
面向对象: 类,对象 函数放在类里,叫方法 封装 #如何调用 1. 创建对象, 类名() obj= Foo() 2. 通过对象去执行方法 obj.mail("leon@me.com" ...
- 自定义textView的高度
原文地址: http://www.cocoachina.com/ios/20141226/10778.html iOS 8 之后的新特性
- 养成代码注释习惯,帮助你更好使用NetBeans导航器
在使用NetBeans编写php代码时,为了在一个类中,或者在方法库文件中快速找到你想要找的函数或方法,通常我们会使用NetBeans的导航器. 我们看一个导航器的事例: 大家知道,在php中代码习惯 ...
- libeXosip2(1) -- Modules
Modules Here is a list of all modules: [detail level 12] The eXtented eXosip stack LibeXosip2 Versio ...
- Base64 加密之中文乱码
ase64编码将二进制数据按照每三个字节转换成四个字节可读字符,编码后的字符长度大约为136.1%.字符范围为 A-Z a-z 0-9 \ +.但编码后的字符串不太适合使用URL传输,中文加密 ...
- FlexComboBoxTree
在我的CSDN资源中有项目工程文件.下载导入工程即可看到效果,下面是地址. http://download.csdn.net/detail/cym_lmy/6326053 MyCombBoxTree1 ...