[LeetCode系列]最大容器问题
给定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系列]最大容器问题的更多相关文章
- leetcode 系列文章目录
leetcode 系列文章目录 0. 两数之和1. 两数相加 2. 无重复字符的最长子串 3. 寻找两个有序数组的中位数 4. 最长回文子串 5. Z 字形变换 6. 整数反转 7. 字符串转换整数 ...
- C#刷遍Leetcode系列连载 索引
C#刷遍Leetcode系列文章 索引 索引(陆续发布中,请保持关注) C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介 C#刷遍Leetcode面试题系列连载(2): No.38 - ...
- Leetcode系列之两数之和
Leetcode系列之两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一个答案.但是,你 ...
- Spring Ioc源码分析系列--Ioc容器BeanFactoryPostProcessor后置处理器分析
Spring Ioc源码分析系列--Ioc容器BeanFactoryPostProcessor后置处理器分析 前言 上一篇文章Spring Ioc源码分析系列--Ioc源码入口分析已经介绍到Ioc容器 ...
- Spring Ioc源码分析系列--Ioc容器注册BeanPostProcessor后置处理器以及事件消息处理
Spring Ioc源码分析系列--Ioc容器注册BeanPostProcessor后置处理器以及事件消息处理 前言 上一篇分析了BeanFactoryPostProcessor的作用,那么这一篇继续 ...
- Docker系列01—容器的发展历程---Docker的生态圈
本文收录在容器技术学习系列文章总目录 Docker 和容器技术的发展可谓是日新月异,本文试图以全局的视角来梳理一下 docker 目前的生态圈.既然是概览,所以不会涉及具体的技术细节. Docker ...
- Docker系列10—容器编排工具Docker Compose详解
本文收录在容器技术学习系列文章总目录 1.Docker Compose 概述 Compose是一个用于定义和运行多容器Docker应用程序的工具.使用Compose,您可以使用Compose文件来配置 ...
- 《Kubernetes与云原生应用》系列之容器设计模式
http://www.infoq.com/cn/articles/kubernetes-and-cloud-native-app-container-design-pattern <Kubern ...
- Kubernetes 普及系列:容器基础入门
随着云原生时代的来临,云以及分布式计算已经是时下最受欢迎的技术之一了.其中 Docker 作为最知名的容器平台,到底有着怎样的魅力来让其无人不知无人不晓?废话不多说,让我们开始逐层掀开容器技术的神秘面 ...
随机推荐
- 搞懂分布式技术4:ZAB协议概述与选主流程详解
搞懂分布式技术4:ZAB协议概述与选主流程详解 ZAB协议 ZAB(Zookeeper Atomic Broadcast)协议是专门为zookeeper实现分布式协调功能而设计.zookeeper主要 ...
- [嵌入式培训 笔记]-----Vim编辑器使用简介
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 第一讲小结 1. 光标在屏幕文本中的移动既 ...
- HDU 3506 (环形石子合并)区间dp+四边形优化
Monkey Party Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Tot ...
- 【MySQL】Error 1264: out of range value for column
此问题是插入的整型数字超出了范围. 比如设置表格的数据类型:cust_fax integer(10) not null 当插入以下数字的时候会抛出标题所说的错误: insert into databa ...
- 编写3ds max插件时遇到的问题总结
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/ 这几天在给公司的美术编写3ds max 2009使用的插件,遇到了一些问题,在此记录一下解 ...
- TCP的time_wait、close_wait状态
转载:http://huoding.com/2013/12/31/316 http://blog.csdn.net/lxnkobe/article/details/7525317 http://k ...
- day6-面向对象基础篇
一.面向对象引子及概念 结合编程的一些理论知识和实践,可以总结出目前存在以下编程模式: 1. 面向过程 按照业务逻辑和实现过程步骤来逐步垒代码,代码编写的逻辑即对应于实际实现的步骤过程,核心是过程两个 ...
- 虚拟机VirtualBox及轻量级的CentOS
1,先下载虚拟机VirtualBox和centos(下边有链接),将VirtualBox安装在本机 2,管理 --> 导入虚拟电脑 --> 选择本地centos文件 3,点击下一步 - ...
- laravel中composer镜像服务的方式
系统全局配置:即将配置信息添加到Composer的全局配置文件config.json中. 单个项目配置:将配置信息添加到某个项目的composer.json文件中. 例1:修改Composer的全局配 ...
- [Android]Adb connection Error:远程主机强迫关闭了一个现有的连接
目前,针对Android手机研发的刷机软件和手机助手软件,都会调用Google开发的adb工具与手机进行通信. 有的刷机软件或者手机助手软件,在系统里会常驻一个服务,独占adb的端口(5037),这样 ...