Leetcode:Container With Most Water分析和实现
题目大意是提供n个非负整数,a1,...,an,可以将其理解为多个边界(垂直的线段),其中ai处于x坐标i处,并且ai代表的线段高度即为ai的值。要求我们取这样的i与j,使得ai与aj以及x坐标轴围成的碗状物能容纳面积最多的水(即abs(i-j)*min(ai,aj)最大)。
最简单的方案就是暴力枚举:
res= -1, maxl = 0, maxr = 0
for(i=0; i < n; i++)
for(j = i + 1; j < n; j++)
res = max(abs(i-j)*min(a[i],a[j]), res)
说一下我的思路。假设函数maxCapacity({ai,...,aj})用于计算ai,...,aj中所能围成的碗状物的最大的保存水的容量,而函数capacity(ai,aj)=abs(i-j)*min(ai,aj)。
对于ai,...,aj,其中1<=i<j<=n:
若ai<aj,那么maxCapacity({ai,...,aj})=max(capacity(ai,aj), maxCapacity({ai+1,...,aj})
若ai>aj,那么maxCapacity({ai,...,aj})=max(capacity(ai,aj), maxCapacity({ai,...,aj-1})
若ai==aj,那么maxCapacity({ai,...,aj})=max(capacity(ai,aj), maxCapacity({ai+1,...,aj-1})
说明一下原因,如果ai和aj围成的碗状物的容量不是最大的,那么结果的边界必定不可能包含ai和aj中的较小值。这是因为对于最终的最优边界ab,ae(e>b),不妨假设ab=min(ai,aj),那么(e-b)*min(ab,ae)<=(e-b)*min(ai,aj)<(j-i)*min(ai,aj)=capacity(ai,aj)。这与ab和ae作为最优边界的定义相悖,因此结果的边界必定不可能包含ai和aj中的较小值。对于ai==aj的情况,ai和aj都可以作为较小值,因此ai和aj都不可能是最优边界之一。故最终的maxCapacity可以这样实现:
maxCapacity(a, i, j):
if(i >= j)
return 0
if(ai < aj)
sub = maxCapacity(a, i+1, j)
else if(ai > aj)
sub = maxCapacity(a, i, j-1)
else
sub = maxCapacity(a, i+1, j-1)
return max((j - i) * min(a[i], a[j]), sub)
整段代码不算上的递归的部分的时间复杂度为O(1),而内部只发生一次递归且每次递归都必定将传入集合的大小减少1,而集合的初始大小为n,因此递归最多会发生n次,故总的时间复杂度为O(1)*n=O(n)。
最后老规矩,贴代码
package cn.dalt.leetcode;
/**
* Created by dalt on 2017/6/17.
*/
public class ContainerWithMostWater {
public int maxArea(int[] height) {
return maxArea(height, 0, height.length - 1);
}
private int maxArea(int[] height, int i, int j) {
int sub;
if (j <= i) {
return 0;
} else if (height[i] < height[j]) {
sub = maxArea(height, i + 1, j);
} else if (height[i] > height[j]) {
sub = maxArea(height, i, j - 1);
} else {
sub = maxArea(height, i + 1, j - 1);
}
return Math.max((j - i) * Math.min(height[i], height[j]), sub);
}
}
Leetcode:Container With Most Water分析和实现的更多相关文章
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- [LeetCode] Container With Most Water 简要分析
前言 这题非要说贪心的话也算是吧,不过最主要的特征还是双指针.LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的.这道题倒是“短板效应”的不错体现了. 题目 题目链接 Given n non-neg ...
- [LeetCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode]Container With Most Water, 解题报告
前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n no ...
- C++LeetCode:: Container With Most Water
本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做.感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过.每天都要被LeetCo ...
- leetcode Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [Leetcode] Container With Most Water ( C++)
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- LeetCode——Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- LeetCode Container With Most Water (Two Pointers)
题意 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
随机推荐
- 《Drools7.0.0.Final规则引擎教程》第3章 3.2 KIE概念&FACT对象
3.2.1 什么是KIE KIE(Knowledge Is Everything),知识就是一切的简称.JBoss一系列项目的总称,在<Drools使用概述>章节已经介绍了KIE包含的大部 ...
- windows C++ 全局异常捕捉函数
windows 核心编程中讲过 SEH 结构化异常处理 ::SetUnhandledExceptionFilter(MyUnhandledExceptionFilter); LONG WINAPI M ...
- 演示使用Metasploit入侵Android
文本演示怎么使用Kali Linux入侵Android手机. Kali Linux IP地址:192.168.0.112:接收连接的端口:443. 同一局域网内android手机一部(android ...
- 如何解决无法通过SSL加密与SQLServer建立连接
在部署项目时,经常会遇到驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接,错误:Java.lang.RuntimeException: Could not gene ...
- sql_自连接,181,182,196,197
181. Employees Earning More Than Their Managers https://leetcode.com/problems/employees-earning-more ...
- HDFS的工作原理扫扫盲
问题导读: 1.什么是分布式文件系统? 2.怎样分离元数据和数据? 3.HDFS的原理是什么? Hadoop分布式文件系统(HDFS)是一种被设计成适合运行在通用硬件上的分布式文件系统.HDFS是一个 ...
- fft蝶形算法的特点
- 洛谷 P3225 [HNOI2012]矿场搭建
传送门 题目大意:建设几个出口,使得图上无论哪个点被破坏,都可以与出口联通. 题解:tarjian求割点 首先出口不能建在割点上,找出割点,图就被分成了几个联通块. 每个联通块,建出口.如果割点数为0 ...
- [转]Mac上的抓包工具Charles
$*********************************************************************************************$ 博主推荐 ...
- Oracle中的存储过程简单例子
--创建表create table TESTTABLE( id1 VARCHAR2(12), name VARCHAR2(32))select t.id1,t.name from TESTTAB ...