LeetCode#11. Container With Most Water
问题描述
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
一个数组,每个代表一个高度,按(i,ai)放在坐标轴上,从中选取两个做一个容器,求最大容积。
暴力O(n2)解法
数组中的元素两两组合,O(n2),超时。
class Solution{
public:
int maxArea(vector<int>& height) {
int i,j,max=0,tmp;
for(int i=0;i<height.size();++i)
for(int j=i+1;j<height.size();++j){
tmp=(j-i)*(height[i]<height[j]?height[i]:height[j]);
max=tmp>max?tmp:max;
}
return max;
}
};
我的O(nlogn)解法
思路
最矮的元素,如果必须选择它作容器,则最大值为最左边的元素加当前元素,或最右边的元素加当前元素。
算法
- 假设当前ai最数组中最矮的高度,最左边可用的高度编号是l,右边是r,则选择ai的容器,其最大容积是max(f(ai,al),f(ai,ar)),即max((i-l)*h[i]),(r-i)*h[i])),与保存的极值max比较,取最大值;
- 去掉当前最矮高度,调整最左和最右可用编号;
- 重新选择最矮高度,重复1;
- 当数组中仅剩下一个元素时,算法结束。
分析
这个思路先对数组排序再进行一次遍历,复杂度时O(nlogn+n),排序带来非常大的开销。另外,我的代码空间开销也很高。
代码
class SolutionOld{
public:
int maxArea(vector<int>& height) {
//1.Sort O(nlogn)
int size=height.size();
vector<pair<int,int> > vec;
for(int i=0;i<size;++i)
vec.push_back(pair<int,int>(height[i],i));
sort(vec.begin(),vec.end());
//2.Travel O(n) 当前最矮的柱子,它的最大容积,是它的高*可达到的最长的长度
int max=0,tmp,left=0,right=size-1;
bool *flag=new bool[size];
for(int i=0;i<size;++i) flag[i]=0;
for(int i=0;i<vec.size();++i){
tmp=vec[i].first*(vec[i].second-left>right-vec[i].second?vec[i].second-left:right-vec[i].second);
max=max>tmp?max:tmp;
flag[vec[i].second]=true;
while(flag[left]) ++left;
while(flag[right]) --right;
if(left>=right) break;
}
return max;
}
};
来自讨论区的O(n)解法
思路
先选取最宽的容器,如果其他容器容积想大于此容器,由于宽度已经减小,高度必须比最宽的容器高。
代码
class Solution {
public:
int maxArea(vector<int>& height) {
int i=0,j=height.size()-1,max=0;
while(i<j){
int curHeight=min(height[i],height[j]);
int tmp=(j-i)*curHeight;
max=max>tmp?max:tmp;
while(curHeight>=height[i]&&i<j) ++i;
while(curHeight>=height[j]&&i<j) --j;
}
return max;
}
};
分析
遍历一遍数组,O(n),思路很棒, 代码十分简洁优雅
LeetCode#11. Container With Most Water的更多相关文章
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- LeetCode 11. 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). ...
- Java [leetcode 11] Container With Most Water
问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- C#解leetcode 11. 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 My Submissions Question 解题思路
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). ...
随机推荐
- PowerDesigner生成Access数据库
1.打开PowerDesigner,设置PowerDesigner的当前数据库为Access: 2.在PowerDesigner中新建表结构(物理模型): 3.PowerDesigner菜单中:Dat ...
- js树形控件—zTree使用总结
0 zTree简介 树形控件的使用是应用开发过程中必不可少的.zTree 是一个依靠 jQuery 实现的多功能 “树插件”.优异的性能.灵活的配置.多种功能的组合是 zTree 最大优点. 0.0 ...
- 自己实现简单的AOP(五)使Demo适应webApi、亦可完成属性自动注入
在前文的Demo中,webApi的Controller是不能自动注入的,原因是 IHttpController 和 IController 是通过两个不同的途径进行激活的. IHttpControll ...
- Win10 UWP系列:更新UWP时注意的问题——TargetDeviceFamily
前几天把CurrencyExchanger提交到微软参加Master认证,结果没有通过,反馈了一些错误,看来微软检查还是比较仔细的. 错误主要有: Visual feedback helps user ...
- 在MVC控制器里面使用dynamic和ExpandoObject,实现数据转义的输出
在很多时候,我们在数据库里面定义表字段和实际在页面中展示的内容,往往是不太匹配的,页面数据可能是多个表数据的综合体,因此除了我们在表设计的时候考虑周到外,还需要考虑数据展现的处理.如果是常规的处理,那 ...
- OA项目——总结
先来张大致结构图: 项目链接:https://github.com/shuai7boy/YM_OA
- 无法为目标平台“Microsoft.Data.Tools.Schema.Sql.Sql120DatabaseSchemaProvider”创建扩展管理器
很久没写博客了,这段时间情绪不那么稳定,还是心态的问题... 就简单写个问题的解决方法吧,其实最近遇到的问题蛮多的,就拿这个解决过后又遇到的来写吧. 正如标题一样:VS2013 无法为目标平台“Mic ...
- php导入excel表格
我们做网站的时候经常要用到excel导入和导出的功能,我们通常的做法是用phpexcel工具包来完成,具体方法如下: html代码: <form action="{:U('Mall/u ...
- Scalaz(58)- scalaz-stream: fs2-并行运算示范,fs2 parallel processing
从表面上来看,Stream代表一连串无穷数据元素.一连串的意思是元素有固定的排列顺序,所以对元素的运算也必须按照顺序来:完成了前面的运算再跟着进行下一个元素的运算.这样来看,Stream应该不是很好的 ...
- ABP 初探 之 AbpSession 扩展
Abp的权限管理是基于 Identity,所有的扩展也是基于 claims .claims 有许多默认属性,具体连接 关于 Identity的详细介绍,可以参考园友博客 继承 Microsoft.As ...