给定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. 微信小程序------轮播图

    swiper 微信小程序实现轮播图,和网站,APP的效果差不多,代码少,效率高. 先来看看效果图: 主要用swiper + swiper-item来实现 <view class='swiper' ...

  2. Calendar时间获取天,周,月,季度,年度时间段

    Date类 Date类的大多数构造函数和方法都已经抛弃,只留下和毫秒有关的操作,目前常用的构造函数 常用方法 设置毫秒数 读取毫秒数 toString()打印结果 //创建日期对象,把当前的毫秒值转成 ...

  3. C++(二十五) — 类的封装、实现

    1.类的封装.实现.对象的定义及使用 (1)类是一组对象的抽象化模型.类对象将数据及函数操作集合在一个实体中,只需要接口,而不需要知道具体的操作. 隐藏细节,模型化: 类内自由修改: 减少耦合,相当于 ...

  4. 设计模式--代理模式C++实现

    代理模式C++实现 1定义 为其他对象提供一种代理以控制对这个对象的访问 2类图 角色定义: Subject抽象主体角色,抽象类或者接口,是一个普通的业务类型定义 RealSubject具体主体角色, ...

  5. UVALive-3713 Astronauts (2-SAT)

    题目大意:有三个任务A.B.C,n个已知年龄的人.A任务只能被年龄不小于平均年龄的人做,B任务只能被平均年龄以下的人做,C任务不限,相互讨厌的两个人不能做同一件任务,现在已知厌恶关系,求一种任务分配方 ...

  6. PyCharm在win10的64位系统安装实例

    搭建环境 1.win10_X64,其他Win版本也可以. 2.PyCharm版本:Professional-2016.2.3. 搭建准备 1.到PyCharm官网下载PyCharm安装包. 2.选择W ...

  7. DB2 设置最大连接数

    db2 connect to dbname user username using passwd db2 update db cfg using MAXAPPLS number 查看最大连接数 查看D ...

  8. bzoj3402

    题解: spfa最短路 然后枚举判断奇数 代码: #include<bits/stdc++.h> using namespace std; ; ]; void jb(int x,int y ...

  9. bzoj3401

    题解: 单调栈 一个一个压入 然后比下面高就弹出 代码: #include<bits/stdc++.h> using namespace std; ; int n,tot,a[N],z[N ...

  10. 2018.11.14 Chopin’s

    The lineaments of Chopin’s肖邦 short, dramatic life are familiar to most classical-music enthusiasts. ...