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 ...
随机推荐
- Photon Cloud Networking: OnPhotonSerializeView Not Firing
Photon Cloud Networking: OnPhotonSerializeView Not Firing http://answers.unity3d.com/questions/31305 ...
- [python] 获得所有的最长公共子序列
两句闲话 得到两个序列的最长公共子序列(LCS)是个经典问题,使用动态规划,实现起来并不难. 一般来说,我们只是输出一个LCS.但是,老师布置的作业是输出所有的LCS. 解法 按照一般的方法,我们首先 ...
- HDU - 5297:Y sequence (迭代&容斥)
Yellowstar likes integers so much that he listed all positive integers in ascending order,but he hat ...
- matlab中freqz的用法以及多项式的展开
对于一个变量a,matlab中定义需要这样 syms a: 定义之后就可以写由变量组成的式子,比如 c=(1+a^-1)^5; 可以用expand(c) 就能把c展开成多项式,每一项的系数就可以看到. ...
- 转载 fpga中 restoring 和 non-restoring 除法实现。
对于non-restoring方法,主要是用rem和den移位数据比较,rem_d长度为den+nom的总长,den_d长度为den+nom的总长度,rem_d的初始值为{{d_width{1'b0} ...
- SPOJLCS Longest Common Substring
题意 A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is th ...
- Oracle备份与恢复案例
注:以下内容来自<Oracle备份与恢复案例.doc>文档. Oracle备份与恢复案例 By Piner 一. 理解什么是数据库恢复 当我们使用一个数据库时,总希望数据库的内容是可靠的. ...
- spring mvc加了@produces注解后报406
问题背景:调用http的post接口返回一个String类型的字符串时中文出现乱码,定位出问题后在@RequestMapping里加produces注解produces = "applica ...
- Linux安装python
1.打开终端,输入:wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0b4.tgz下载完毕后 2.输入解压命令:tar –zxvf Py ...
- gen_fsm的学习笔记
网上搜索gen_fsm的例子,90%都是code_lock,依葫芦画瓢弄了下,记录一些学习心得 init(UnLockCode) -> process_flag(trap_exit,true), ...