011 Container With Most Water 盛最多水的容器
给定 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) 。画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
详见:https://leetcode.com/problems/container-with-most-water/description/
实现语言:Java
方法一:暴力解,超时
class Solution {
public int maxArea(int[] height) {
int size=height.length;
if(size<2){
return 0;
}
int maxArea=0;
for(int i=0;i<size;++i){
for(int j=i+1;j<size;++j){
int area=height[i]<height[j]?height[i]*(j-i):height[j]*(j-i);
if(area>maxArea){
maxArea=area;
}
}
}
return maxArea;
}
}
方法二:时间复杂度:O(n),空间复杂度:O(1)
从第一个高度为起始容器壁,以最后一个高度为终止壁,如果a1 <= an,那么以a1为起始的容器最大是a1 * (n - 1),以a1为容器壁的最大容器计算出来的。那么以a1为壁的所有情况不需要再考虑,接着考虑a2的;同理,如果a1 > an,an不再考虑,考虑an-1,这有点类似"夹逼定理"。比较ai和aj(i<j)如果ai <= aj,i++;否者j ++直到i == j。这个算法的时间复杂度是(O(n))。
class Solution {
public int maxArea(int[] height) {
int size=height.length;
if(size<2){
return 0;
}
int maxArea=0;
int start=0;
int end=size-1;
while(start<end){
maxArea=Math.max(maxArea,Math.min(height[start],height[end])*(end-start));
if(height[start]<height[end]){
++start;
}else{
--end;
}
}
return maxArea;
}
}
011 Container With Most Water 盛最多水的容器的更多相关文章
- 【LeetCode】11. Container With Most Water 盛最多水的容器
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...
- [LeetCode]11. Container With Most Water 盛最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- Leetcode11.Container With Most Water盛最多水的容器
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...
- [LeetCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode] 11. Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- [LintCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- LeetCode:盛最多水的容器【11】
LeetCode:盛最多水的容器[11] 题目描述 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 ...
- Java实现 LeetCode 11 盛最多水的容器
11. 盛最多水的容器 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) ...
- 力扣Leetcode 11. 盛最多水的容器
盛最多水的容器 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找 ...
随机推荐
- Guava手记
Cache Guava的Cache封装的功能比较全面,但是很多地方和设想的不太一样,最明显的就是RemovalListener,它并不是invalid之后就会被调用,因为在调用Cache的invali ...
- 洛谷【P1009】阶乘之和
题目传送门:https://www.luogu.org/problemnew/show/P1009 高精度加法:https://www.cnblogs.com/AKMer/p/9722610.html ...
- Ruby代码块(Block)
1.什么是代码块 在Ruby中,{}或do...end之间的代码是一个代码块.代码块只能出现在一个方法的后边,它紧接在方法最后一个参数的同一行上,由yield关键字调用.例如: [1,2,3,4,5] ...
- IIS应用池保持激活工具开发
之前的 开源一个定时任务调度器 webscheduler 中涉及的定时调用应用已经开始在正式环境中启用,发布到IIS下进行测试,发现一旦应用长时间没有访问(大约半个多小时)就会引发 Applicati ...
- linux日常管理-sar工具
查看网卡瓶颈 查看网卡流量 默认10分钟一次 查看实时流量 每秒钟显示一次 显示5次 网卡有 lo eth0 主要看eth0外网 rxbyt/s 进网口和 txbyt/s出网口 带宽看txby ...
- poi包的几行基本的设置单元格样式
——杂言:写过很多遍这段代码,今天姑且记录一下,便于翻阅. jar:poi-3.7.jar 注意:第8行应该调用cellStyle.setFillForeGroundColor(HSSFColor.G ...
- JConsole远程监控配置
首先,看本机(Windows)安装了JRE没 Win > CMD 打开命令窗口 如有安装,则会显示以下版本信息:若没有显示,就安装吧 C:\Users\Administrator>java ...
- javascript深入理解js闭包(转)
javascript深入理解js闭包 转载 2010-07-03 作者: 我要评论 闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. ...
- HDU 3966 Aragorn's Story (简单树链剖分)
题意:给一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路径上的所有点权值加上K D C1 C2 K:把C1与C2的路径上的所有点权值减去K Q C:查询节点编号为C ...
- 新安装的 ubuntu 下 make menuconfig 报错
环境:Ubtuntu 12.04 LTS 新安装的ubuntu 出现不能使用make menuconfig. 1.sudo apt-get update 更新软件 2.安装下面的软件 sudo apt ...