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. [MySQL]快速解决"is marked as crashed and should be repaired"故障

    具体报错如下: Table '.\Tablename\posts' is marked as crashed and should be repaired 提示说论坛的帖子表posts被标记有问题,需 ...

  2. Robotium学习笔记一

    一. 重签名问题 1.从手机Pull所需的apk通过压缩工具删除META-INF目录 2.通过以下命令行进行签名 >jarsigner -keystore "C:\Documents ...

  3. 多目标遗传算法 ------ NSGA-II (部分源码解析) 临时种群生成新父代种群 fillnds.c

    /* Nond-domination based selection routines */ # include <stdio.h> # include <stdlib.h> ...

  4. 第一个java程序

    完成自己的第一个java程序 1.新建一个文本文档,在文本文档中编写自己第一个java程序的代码,代码如下; class hello { public static void main(String[ ...

  5. XML 语法规则

    转摘自:http://www.w3school.com.cn/xml/xml_elements.asp XML 语法规则 XML 文档包含 XML 元素. XML 的语法规则很简单,且很有逻辑.这些规 ...

  6. Android ArrayAdpater 填充集合

    在这里我们新创建一个任务来演示 然后再布局文件中定义一个ListView 来当作显示数据的容器,布局文件代码如下: <LinearLayout xmlns:android="http: ...

  7. C++ CheckListBox

    实现过程 CCheckListBox    listbox1;     listbox1.AddString("葡萄");     listbox1.AddString(" ...

  8. eclipse设置快捷键sysout+Alt+/后出System.out.println!亲測可用!

    曾经一直用myeclipse,没有这方面的顾虑,如今换到了eclipse,非常多要自己设置了,比方非常多快捷键. 最经常使用的,执行到sysout ,eclipse总是不自己主动补全,非常是恼火!!! ...

  9. 分布式助手Zookeeper(二)

    分布式助手Zookeeper(二)博客分类: Zookeeper zookeeperzookeeper的安装和配置观察者observer 散仙在上篇文章介绍了,zookeeper的一系列基础知识,如果 ...

  10. mongodb的地理位置索引

    mongoDB支持二维空间索引,使用空间索引,mongoDB支持一种特殊查询,如某地图网站上可以查找离你最近的咖啡厅,银行等信息.这个使用mongoDB的空间索引结合特殊的查询方法很容易实现.前提条件 ...