[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 ...
随机推荐
- KMP算法———模板
做出KMP字符串匹配算法心情也是好好哒,萌萌哒. 感谢黄学长,感谢栋栋! #include<cstdio>#include<string>#include<iostrea ...
- 快速搭建Android 开发环境-使用ADT Bundle
一.搭建Android开发环境 近日要学Android开发基础,就着手搭建Windows下的Android开发环境. 找了一些相关的博文参考,基本上都是要分别下载和安装JDK, Eclipse, An ...
- PAT 64.Complete Binary Search Tree
题目链接: http://pat.zju.edu.cn/contests/pat-a-practise/1064 思路分析: 1)先对数组排好序. 2)采用中序遍历的方式,将排好序的元素逐个插入在完全 ...
- HEAP[xxx.exe]:Invalid Address specified to RtlValidateHeap 错误的解决方法总结
一.情况 抽象出问题是这样的: class DLL_API1 A { func() { vector vec; B b; b.func(vec); return TRUE; } } 其中B是另一个导出 ...
- [Mugeda HTML5技术教程之14]案例分析:制作网页游戏
本文档要分析的案例是一个爱消除的网页小游戏,从中可以体会一些Mugeda API的用法和使用Mugeda动画制作网页游戏的方法. (一)游戏规则: 1.开始游戏时,手机出现在最上面一行的任意一格: 2 ...
- Go and JSON
Go and JSON 在使用Go开发web项目的过程中, 数据库读写操作与JSON格式的输入输出是两块最基础的模块, Go的标准库已经帮我们做了很多, 熟悉database/sql与encoding ...
- Android手机配置gcc,实现手机编译代码
1.下载gcc.zip 2.把gcc.zip解压存放在/data目录下(也可以是其他目录,看个人习惯) 3.配置gcc环境变量 export GCCHOME=/data/gcc (gcc存放路径) e ...
- mysql group_concat函数是有长度限制的
在表关联查询中,特别是一对多关系的表查询中,group_concat函数是很有用的一个函数,帮助我们减少对数据库查询的次数,减少服务器的压力. 但是今天使用group_concat函数查询数据库时,发 ...
- iPhone 和Android应用,特殊的链接:打电话,短信,email;
http://ice-k.iteye.com/blog/1426526 下面的这篇文章主要是说,网页中的链接如何写,可以激活电话的功能. 例如,页面中展示的是一个电话号码,当用户在手机浏览器里面点击这 ...
- poj3261 -- Milk Patterns
Milk Patterns Time Limit: 5000MS ...