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 ...
随机推荐
- 《Ubuntu入门基础》第三篇
创建虚拟磁盘
- haroopad 语法高亮问题
<!DOCTYPE html> Untitled.html div.oembedall-githubrepos{border:1px solid #DDD;border-radius:4p ...
- Angular在render完成之后,执行Js脚本
AngularJs中,如何在render完成之后,执行Js脚本 app.directive('onFinishRenderFilters', function ($timeout) { return ...
- 【前端】HTML入门笔记
教程 HTML 指的是超文本标记语言 (Hyper Text Markup Language).使用标记标签来描述网页 HTML 提示:使用小写标签\属性\属性参考手册\HTML颜色\HTML颜色名 ...
- 编写实现字符串拷贝函数strcpy()完整版
有个题目编程实现字符串拷贝函数strcpy(),很多人往往很快就写出下面这个代码. void strcpy( char *strDest,char *strSrc ) { while(( *strDe ...
- BZOJ4560 [JLoi2016]字符串覆盖
题意 字符串A有N个子串B1,B2,-,Bn.如果将这n个子串分别放在恰好一个它在A中出现的位置上(子串之间可以重叠) 这样A中的若干字符就被这N个子串覆盖了.问A中能被覆盖字符个数的最小值和最大值. ...
- excel oracle字段命名(大写下划线分词)转 驼峰命名
干货: (帕斯卡) =LEFT(C251,1)&MID(SUBSTITUTE(PROPER(C251),"_",""),2,100) (驼峰) =LOW ...
- sqlserver sql语句附加 分离数据库
当使用 sp_attach_db 系统存储过程附加数据库时- - Tag: 当使用 sp_attach_db 系统存储过程附加数据库时 //附加数据库 sp_attach_db 当使用 sp_atta ...
- 随时查找中位数——pat1057
http://pat.zju.edu.cn/contests/pat-a-practise/1057 题目的意思是可以在一个可以任意添加于删除整数的集合里随时查找该集合的中位数 每次查找用nlogn的 ...
- python3 之 linux命令实现
os.mkdir(path[, mode]) 以数字mode的mode创建一个名为path的文件夹.默认的 mode 是 0777 (八进制) # 创建多级目录 mkdir -p dir1/dir2 ...