leetcode problem 11 Container With Most Water
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.
思路: 设Hight[i]为第i个line的高度,设立两个指针left和right初始分别指向最左端和最右端,maxArea由此得到初始面积。然后左指针不断右移,右指针不断左移直到他们相交并由此更新最大面积。移动规则如下:
if (Height[left] < Height[right])
left++
else
right--
即如果左指针小,那么左指针右移。如果右指针小,那么右指针左移。
先贴出代码,再证明为什么这种移动法能够得到最大面积。
class Solution {
public:
int maxArea(vector<int> &height) {
int left = , right = height.size()-;
int max_area= ;
while (left < right) {
int tmp = area(min(height[left], height[right]), right - left);
if (max_area < tmp)
max_area = tmp;
if (height[left] < height[right])
++left;
else
--right;
}
return max_area;
}
private:
int area(int x, int y){
return x*y;
}
};
下面证明这种方法可以得到最大面积:
首先我们需要知道,left和right指针一定会遍历所有端点,且它们不会有公共的遍历点。
然后我们可以先假设已经知道了最大面积,并设其左端点为leftMax,右端点为rightMax,且不妨设Height[leftMax] < Height[rightMax]。我们需要证明的是存在某一个时刻使得left=leftMax and right=rightMax
由于两个端点构成了最大面积,那么可以得到
(1) Height[left] < Height[leftMax] (1 <= left < leftMax) 且
(2 ) Height[right] < Height[leftMax] (leftMax+1 <=right <=totalNum)
根据假设:
(3) Height[leftMax] < Height[rightMax]
那么根据算法的规则:如果左端点比右端点小,左端点右移,反之亦然。那么由此可得:
(4)如果left先到达leftMax,而right还未到达rightMax,那么根据(2),right一定会一直移动到rightMax而left呆在leftMax不动
(5)如果right先到达rightMax,而left还未达到leftMax,那么根据(1)和(3),left一定会一直移动到leftMax而right呆在rightMax不懂。
根据(4)和(5)得到一定存在某一个时刻使得left=leftMax and right=rightMax。
leetcode problem 11 Container With Most Water的更多相关文章
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode】11. Container With Most Water 盛最多水的容器
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...
- 【LeetCode】11. Container With Most Water
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- LeetCode OJ 11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- Leetcode Array 11 Container With Most Water
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- LeetCode Array Medium 11. Container With Most Water
Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...
- leetcode面试准备:Container With Most Water
leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...
随机推荐
- oc学习之路----APNS消息推送从证书到代码(2015年4月26号亲试可用)
前言:看这篇博客之前要准备:首先的有一个99刀的个人开发者账号或者199刀的企业开发者账号,其次你用的是apns消息推送,(本人之前四处打听有没有其他消息推送的方法:收获如下:首先如果想做到apns的 ...
- 跟我学SpringMVC目录汇总贴、PDF下载、源码下载
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- PERCONA-TOOLKIT 工具的安装与使用2
[root@server-mysql ~]# cd /usr/bin [root@server-mysql bin]# ls pt* pt-align pt-duplicate-key-checker ...
- careercup-树与图 4.3
4.3 给定一个有序整数数组,元素各不相同按升序排列,编写一个算法,创建一棵高度最小的二叉查找树. 解答 想要使构建出来的二叉树高度最小,那么对于任意结点, 它的左子树和右子树的结点数量应该相当.比如 ...
- list add元素覆盖之前元素问题思考
最近码墙时发现了一个很有意思的问题,定义一个引用对象,如果在循环外面定义对象,在循环里list.add(对象),最后的结果却是所有的对象值都是一样的,即每add一次,都会把之前所有的数据覆盖掉,蛮有趣 ...
- MAC地址查询 Linux/Unix操作系统mac地址怎么查
Linux/Unix操作系统熟悉的人都通常是用console命令控制台来进行相应的操作. Linux/Unix操作系统查看网卡mac地址的方法可以通过以下命令获得: 1.ifconfig -a 2. ...
- SoundPool没有声音的问题
在项目中需要播放一个提示,很短的一个声音,Android中播放声音有两种方式:MediaPlayer和SoundPool.相对来说SoundPool比较轻量级一些,多用在播放比较短急的声音,Media ...
- Xcode快捷键、小技巧与xib圆角设置
快捷键: 1.StoryBoard技巧 当你想直接在view中选择自己想要的元素时,但是又碍于一个view上叠加的元素太多很难直接选中,那么在这时,你同时按住键盘上的shift和 control键,然 ...
- verilog语法:进程名
always @ (posedge clk, negedge rstn)begin:State_flipflops if(!rstn) y<=S1; else y<=Y;end请问这段程序 ...
- 闲话:你今天OO了吗?
如果你的分析习惯是在调研技术的时候最先弄清楚有多少业务流程,先画出业务流程图,然后顺藤摸瓜,找出业务流程中每一步骤的参与部门或岗位,弄清楚在这一步参与者所做的事情和填写表单的结果,并关心用户是如何把这 ...