问题描述

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)解法

思路

最矮的元素,如果必须选择它作容器,则最大值为最左边的元素加当前元素,或最右边的元素加当前元素。

算法

  1. 假设当前ai最数组中最矮的高度,最左边可用的高度编号是l,右边是r,则选择ai的容器,其最大容积是max(f(ai,al),f(ai,ar)),即max((i-l)*h[i]),(r-i)*h[i])),与保存的极值max比较,取最大值;
  2. 去掉当前最矮高度,调整最左和最右可用编号;
  3. 重新选择最矮高度,重复1;
  4. 当数组中仅剩下一个元素时,算法结束。

分析

这个思路先对数组排序再进行一次遍历,复杂度时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的更多相关文章

  1. 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 用双指针向中间滑动,较小的高度就作为当前情 ...

  2. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  3. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  4. LeetCode 11. Container With Most Water (装最多水的容器)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  5. [LeetCode] 11. Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

  6. Java [leetcode 11] Container With Most Water

    问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...

  7. C#解leetcode 11. Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  8. [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).  ...

  9. [leetcode]11. Container With Most Water存水最多的容器

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

随机推荐

  1. 1.Struts2简介和Struts2开发环境搭建

    一.Struts2简介: 1.什么是Struts2? 著名的SSH三大框架分别为:表现层(Struts).业务逻辑层(Spring),持久化层(Hibernate). Struts2是在WebWork ...

  2. JSP自定义tag

    前端需要调用后端的配置,想起velocity-tools.然而jsp的话,目前只能想到tag和EL表达式了. Tag相当好写,jsp2.0提供了简化写法: 编写一个java类: public clas ...

  3. Struts2 源码分析——调结者(Dispatcher)之执行action

    章节简言 上一章笔者写关于Dispatcher类如何处理接受来的request请求.当然读者们也知道他并非正真的执行action操作.他只是在执行action操作之前的准备工作.那么谁才是正真的执行a ...

  4. CSS魔法堂:Box-Shadow没那么简单啦:)

    前言  说起box-shadow那第一个想法当然就是用来实现阴影,其实它还能用于实现其他好玩的效果的,本篇就打算说说box-shadow的那些事. 二话不说看效果 3D小球 <style typ ...

  5. Premiere Pro & After Effects插件开发调试方法

    在给Adobe Premiere Pro(PR)和Adobe After Effects(AE)插件开发时,对于实时调试插件有着很强的需求.除了业务需求外,单步调试插件还能够摸清楚Plugin和Hos ...

  6. ios7.1 in-house app的发布方法

    iOS7.1版本的in-house app必须发布到https站点才能下载安装,原来的连接: itms-services://?action=download-manifest&url=htt ...

  7. 固定在网页顶部跟随滚动条滑动而滑动的DIV层

    在一个页面放2个悬浮框,悬浮框随页面的上下滚动有上下波动的效果,最终固定在同一位置 体验效果:http://hovertree.com/texiao/jsstudy/1/ 代码如下: <!DOC ...

  8. [Excel] WorkBook.SaveAs

    ExcelApplication.WorkBook.SaveAs(Filename, FileFormat, Password, WriteResPassword, ReadOnlyRecommend ...

  9. sessionState与会话

    HTTP 是一种无状态协议.这意味着 Web 服务器会将针对页面的每个 HTTP 请求作为独立的请求进行处理.ASP.NET 会话状态将来自限定时间范围内的同一浏览器的请求标识为一个会话,并提供用于在 ...

  10. php实现设计模式之 观察者模式

    代码片段一: <?php /** * 观察者模式:定于对象间的一种一对多的依赖关系,当一个对象发生改变时,所有依赖它的对象都收到通知并自动更新. */ //例子:少林方丈的通讯录,当扫地僧的号码 ...