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的更多相关文章

  1. 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  2. 【LeetCode】11. Container With Most Water 盛最多水的容器

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...

  3. 【LeetCode】11. Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  4. LeetCode OJ 11. Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  5. Leetcode Array 11 Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  6. 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 用双指针向中间滑动,较小的高度就作为当前情 ...

  7. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  8. LeetCode Array Medium 11. Container With Most Water

    Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...

  9. leetcode面试准备:Container With Most Water

    leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...

随机推荐

  1. 视频播放(iOS开发)

    视频播放 一.视频播放介绍(5种实现方案) AVPlayer 优点 可以自定义UI,进行控制 缺点 单纯的播放,没有控制UI,而且如果要显示播放界面,需要借助AVPlayerLayer,添加图层到需要 ...

  2. POJ3155 Hard Life

    Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 8482   Accepted: 2461 Case Time Limit:  ...

  3. IOS GCD 使用(一) 简介

    一 GCD简介 GCD(Grand Central Dispatch)是苹果为实现并发编程提供的新技术.从基本功能上讲,GCD有点像NSOperationQueue,他们都允许程序将任务切分为多个单一 ...

  4. [Javascript] Gradient Fills on the HTML5 Canvas

    window.onload = function() { var canvas = document.getElementById("canvas"), context = can ...

  5. 【转】针对iOS VS. Android开发游戏的优劣——2013-08-25 17

    http://game.dapps.net/gamedev/experience/8670.html 问题:如果你正在一个新工作室开发一款新的平板/手机游戏,你会选择iOS还是Android? 回答: ...

  6. 从数组中随机取n条不重复的数据

    工作中经常遇到有关数组的一些操作 1. 从数据中随机取n条不重复的数据 (PS:下面的S.each是KISSY.each方法,大家可以改为for循环) /* 1 从数组arr中随机取n条不重复的数据 ...

  7. hibernate缓存技术

    1.缓存 2.Hibernate 缓存作用:为了提高查询效率. 3.第一次操作某个对象的时候,把操作的对象数据存储到缓存中,然后下一次在对同一个对象操作的时候,就不会在连接数据库. 4.Hiberna ...

  8. java_method_删除事务回滚

    public String[] deleteEPGroup(String groupID, String groupName) { String[] operRes=new String[3]; if ...

  9. ThinkPHP函数详解:F方法

    我们已经了解了ThinkPHP中的S方法的用法,F方法其实是S方法的一个子集功能,仅用于简单数据缓存,并且只能支持文件形式,不支持缓存有效期,因为采用的是PHP返回方式,所以其效率较S方法较高,因此我 ...

  10. Lucene.net常用功能说明

    Lucene.net是一个.net下的全文检索类库.配置简单,功能丰富,比较成熟.我在项目中用Lucene.net有一段时间了,这里我把常用一些功能写出来,与大家一起分享. Lucene.net用的是 ...