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). ...
随机推荐
- 【JUC】JDK1.8源码分析之ConcurrentLinkedQueue(五)
一.前言 接着前面的分析,接下来分析ConcurrentLinkedQueue,ConcurerntLinkedQueue一个基于链接节点的无界线程安全队列.此队列按照 FIFO(先进先出)原则对元素 ...
- 理解领域模型Domain Model
定义 业务对象模型(也叫领域模型 domain model)是描述业务用例实现的对象模型.它是对业务角色和业务实体之间应该如何联系和协作以执行业务的一种抽象.业务对象模型从业务角色内部的观点定义了业务 ...
- 在Pycharm中使用GitHub
Pycharm是当前进行python开发,尤其是Django开发最好的IDE.GitHub是程序员的圣地,几乎人人都在用. 本文假设你对pycharm和github都有一定的了解,并且希望在pycha ...
- 2.简单的Code First例子(EF Code-First系列)
现在假想,我们想要为讴歌学校创建一个应用程序,这个程序需要能够来添加或者更新学生,分数,教师还有课程信息. 代替之前我们的做法:先是创建数据库,现在我们不这么做,我们先来创建领域类,首先我来创建两个简 ...
- C++ 版本的 行为树的简单实现
如果你想转载这篇文章呢,请严格按照以下格式注明出处和作者 出处:http://www.cnblogs.com/anxin1225/p/4827294.html 作者:Anxin1225.Bianchx ...
- C# 超时工具类 第二版
附源码,没有附测试demo 之前的工具类:C# 给某个方法设定执行超时时间 /// <summary> /// 超时工具 /// </summary> public class ...
- Redis命令拾遗四——集合类型(命令补充)
补充下上篇文章集合的命令. 上篇地址 博客园蜗牛 http://www.cnblogs.com/tdws/p/5785939.html SCARD Key获得执行集合中元素的数量. SDIFFSTOR ...
- PHP函数之自定义函数
像数学中的函数一样,y=f(x)是函数基本的表达形式,x可看做是参数,y可看做是返回值,即函数定义就是一个被命名的.独立的代码段,它执行特定的任务,并可能给调用它的程序返回一个值. 自定义函数 函数的 ...
- 【转】PHP 杂谈《重构-改善既有代码的设计》之一 重新组织你的函数
原文地址: PHP 杂谈<重构-改善既有代码的设计>之一 重新组织你的函数 思维导图 点击下图,可以看大图. 介绍 我把我比较喜欢的和比较关注的地方写下来和大家分享.上次我写 ...
- rest api参数与content-type
最近为项目组提供rest api 时遇到了关于接口参数的传递问题,主要是没有充分考虑到第三方调用者的使用方式,应该尽量的去兼容公司之前提供出去的接口调用方式,这样可以降低第三方调用者的学习成本,尽管之 ...