[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 作为最知名的容器平台,到底有着怎样的魅力来让其无人不知无人不晓?废话不多说,让我们开始逐层掀开容器技术的神秘面 ...
随机推荐
- 十八 Python分布式爬虫打造搜索引擎Scrapy精讲—Scrapy启动文件的配置—xpath表达式
我们自定义一个main.py来作为启动文件 main.py #!/usr/bin/env python # -*- coding:utf8 -*- from scrapy.cmdline import ...
- 完整的保存onetab的书签信息
之前在百度上找怎么完整的保存onetab的书签信息,没找到答案,于是乎上了google,在stackoverflow上找到了一个相关的答案 How to restore OneTab list of ...
- 奔跑吧ansible笔记一(概述)
1.普通用户想使用sudo到root用户下执行一些有root权限的操作需要在被管理机器上做如下操作 1.切换到root用户下,怎么切换就不用说了吧,不会的自己百度去. 2.添加sudo文件的写权限,命 ...
- Hibernate入门1. Hibernate基础知识入门
Hibernate入门1. Hibernate基础知识入门 20131127 前言: 之前学习过Spring框架的知识,但是不要以为自己就可以说掌握了Spring框架了.这样一个庞大的Spring架构 ...
- String.Remove
String.Remove方法注意事项: 1.该方法不改变元字符串: 2.str = ‘’: str.Remove(str.Length-3);明显超限,但是不报错,返回值为''; str = '1 ...
- TStringHelper.Split
作为对泛型的支持,TStringHelper.Split方法理所应当地出现了. 示例代码1: var iText: string; iAStr: TArray<string>; I: ...
- Jqeury ajax 调用C#的后台程序
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JQueryTest.aspx. ...
- 如何在非Spring管理的类中使用Spring加载的bean
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> < ...
- L163
Chickens slaughtered in the United States, claim officials in Brussels, are not fit to grace Europea ...
- C#设置System.Net.ServicePointManager.DefaultConnectionLimit,突破Http协议的并发连接数限制
在Http协议中,规定了同个Http请求的并发连接数最大为2. 这个数值,可谓是太小了. 而目前的浏览器,已基本不再遵循这个限制,但是Dot Net平台上的 System.Net 还是默认遵循了这个标 ...