给定n个数: a1, a2, ... , an. 代表着(i, ai)个点, 连接这些点与对应的(i, 0), 我们可以得到n条线. 请在这n条线中找出2条, 使得这两条线和x轴构成的容器能够容纳最多的水.

本题解法时间复杂度为O(n), 作者是n00tc0d3r.

我们使用2个指针从数组前后开始遍历, 每次循环都计算一次最大值.

当左侧的数比右侧的数小时, 左指针向右挪动一格, 否则, 右指针向左挪动一格.

直到两个指针相遇, 算法结束.

 class Solution {
public:
int maxArea(vector<int> &height) {
if (height.size() < ) return ;
int maxA = ;
int low = , high = height.size() - ;
while (low < high) {
int A = (high - low) * min(height[low], height[high]);
maxA = max(A, maxA);
if (height[low] < height[high])
low++;
else
high--;
}
return maxA;
}
};

数学证明:

给定a1,a2,a3.....an为输入数组,假设a10和a20是最大面积解,我们需要证明的是左指针指向a10时右指针有机会指向a20. 即证明:当左指针指向a10,右指针指向a21时,下一步一定是移动右指针到a20。

因为永远移动的是较小的一边,分为2种情况,如果a10 > a21,应该移动a21到a20,证毕;如果a10 < a21,那a10和a21的面积至少是a10 * 11,而a10和a20的面积最多是a10 * 10(因为a10小于a21,即使a20大于a10,面积依旧是a10 * 10,如果a20小于a10,则面积小于a10 * 10),从而a10和a21是最优解,和我们的假设矛盾。

综上, 算法返回值必定是最优解.

英文原版说明:

[Given a1,a2,a3.....an as input array. Lets assume a10 and a20 are the max area situation. We need to prove that a10 can be reached by left pointer and during the time left pointer stays at a10, a20 can be reached by right pointer. That is to say, the core problem is to prove: when left pointer is at a10 and right pointer is at a21, the next move must be right pointer to a20.

Since we are always moving the pointer with the smaller value, i.e. if a10 > a21, we should move pointer at a21 to a20, as we hope. Why a10 >a21? Because if a21>a10, then area of a10 and a20 must be less than area of a10 and a21. Because the area of a10 and a21 is at least height[a10] * (21-10) while the area of a10 and a20 is at most height[a10] * (20-10). So there is a contradiction of assumption a10 and a20 has the max area. So, a10 must be greater than a21, then next move a21 has to be move to a20. The max cases must be reached.

]

[LeetCode系列]最大容器问题的更多相关文章

  1. leetcode 系列文章目录

    leetcode 系列文章目录 0. 两数之和1. 两数相加 2. 无重复字符的最长子串 3. 寻找两个有序数组的中位数 4. 最长回文子串 5. Z 字形变换 6. 整数反转 7. 字符串转换整数 ...

  2. C#刷遍Leetcode系列连载 索引

    C#刷遍Leetcode系列文章 索引 索引(陆续发布中,请保持关注) C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介 C#刷遍Leetcode面试题系列连载(2): No.38 - ...

  3. Leetcode系列之两数之和

    Leetcode系列之两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一个答案.但是,你 ...

  4. Spring Ioc源码分析系列--Ioc容器BeanFactoryPostProcessor后置处理器分析

    Spring Ioc源码分析系列--Ioc容器BeanFactoryPostProcessor后置处理器分析 前言 上一篇文章Spring Ioc源码分析系列--Ioc源码入口分析已经介绍到Ioc容器 ...

  5. Spring Ioc源码分析系列--Ioc容器注册BeanPostProcessor后置处理器以及事件消息处理

    Spring Ioc源码分析系列--Ioc容器注册BeanPostProcessor后置处理器以及事件消息处理 前言 上一篇分析了BeanFactoryPostProcessor的作用,那么这一篇继续 ...

  6. Docker系列01—容器的发展历程---Docker的生态圈

    本文收录在容器技术学习系列文章总目录 Docker 和容器技术的发展可谓是日新月异,本文试图以全局的视角来梳理一下 docker 目前的生态圈.既然是概览,所以不会涉及具体的技术细节. Docker ...

  7. Docker系列10—容器编排工具Docker Compose详解

    本文收录在容器技术学习系列文章总目录 1.Docker Compose 概述 Compose是一个用于定义和运行多容器Docker应用程序的工具.使用Compose,您可以使用Compose文件来配置 ...

  8. 《Kubernetes与云原生应用》系列之容器设计模式

    http://www.infoq.com/cn/articles/kubernetes-and-cloud-native-app-container-design-pattern <Kubern ...

  9. Kubernetes 普及系列:容器基础入门

    随着云原生时代的来临,云以及分布式计算已经是时下最受欢迎的技术之一了.其中 Docker 作为最知名的容器平台,到底有着怎样的魅力来让其无人不知无人不晓?废话不多说,让我们开始逐层掀开容器技术的神秘面 ...

随机推荐

  1. flask学习(十二):for循环遍历

    一. 字典的遍历 语法和python一样,可以使用items().keys().values().iteritems().iterkeys().itervalues() {% for k, v in ...

  2. Highcharts 散点图

    Highcharts 散点图 配置 chart 配置 配置 chart 的 type 为 'scatter' .chart.type 描述了图表类型.默认值为 "line". ch ...

  3. 创建自定义JSR303的验证约束(Creating custom constraints)

    转载:http://clongjava.iteye.com/blog/1317649 由于输入验证在软件开发中是必须的一件事情,特别是与用户交互的软件产品,验证用户的潜在输入错误是必不可少的一件事情, ...

  4. 利用expect和sshpass完美非交互性执行远端命令

    yum install expect -y expect 参考:http://blog.csdn.net/snow_114/article/details/53245466 yum install s ...

  5. mifi随身wifi选购

    一款优秀的随身wifi不光要信号好,更要电量足 ,网速快.影响这个三个问题的主要因素就是cpu.so咱们从cpu的角度来分析下mifi 机器型号(cpu型号) TP 961 52000 (MDM962 ...

  6. CUDA库函数使用笔记与案例(一)

    项目合作中需要整合对方公司提供的CUDA代码,因此需要详细学习代码中涉及的cuda函数. CUDA Tool Kit 8.0较完整的官方说明文档: http://docs.nvidia.com/cud ...

  7. L150 Mystery Illness Causing Paralysis in Children Baffles Doctors

    Federal and state health officials are baffled by a mysterious and rare illness that seems to target ...

  8. PostgreSQL CheckPoint设置(转)

    今天在研究checkpoint process的问题时,顺便复习了一下checkpoint设置问题,又有新的疑惑了. checkpoint又名检查点,在oracle中checkpoint的发生意味着之 ...

  9. javaScript高程笔记--最佳实践

    1.可维护性 <1>什么是可维护的代码 (1)可理解性 (2)直观性 (3)可适应性 (4)可扩展性 (5)可调试性 <2>代码约定 (1)可读性---适当的进行注释[函数和方 ...

  10. RxJava 1.x 笔记:过滤型操作符

    我真的是奇怪,上下班的路上看书.看文章学习的劲头特别大,到了周末有大把的学习时间,反而不珍惜,总想打游戏,睡前才踏踏实实地写了篇文章,真是服了自己! 本文内容为 RxJava 官方文档 学习笔记 作者 ...