问题描述

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. Zend Studio主题的设置

    用了两年的Zend Studio,一直是默认的白色主题,让人都产生了视觉疲劳,今天才发现Zend Studio的设置方法: 步骤1:help–>install new software…

  2. 【原创】轻量级移动端即时通讯技术 MobileIMSDK 发布了

    申明:MobileIMSDK目前为个人原创开源工程,投入了大量的时间和精力,希望对需要的人有所帮助.如需与作者交流,见文章底部个人签名处,互相学习.Q群:215891622,欢迎共同志趣者学习和交流. ...

  3. .Net语言 APP开发平台——Smobiler学习日志:实现手机上常见的ListMenuView

    最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的"S ...

  4. GitHub-版本控制

    GitHub的使用:注册,登录,邮箱激活,创建第一个repository.如果branch是master,修改,提交,master直接改变. 稳妥起见创建另一个branch,修改,提交,再发出pull ...

  5. C#测试题若干,都是基础阿

    类的以下特性中,可以用于方便地重用已有的代码和数据的是( ).   A.多态B.封装C.继承D.抽象 答案:http://hovertree.com/tiku/bjaf/a3k6pgq5.htm 可用 ...

  6. python处理空格脚本

    博客园上传代码时拷贝vs里面的代码不能直接粘贴,否则空格会不符合要求 去掉空格代码 # -*- coding: utf-8 -*- '''打开delSpace.txt文本并删除每行开头的八个空格''' ...

  7. PHP intval()

    定义和用法 获取变量的整数值,允许以使用特定的进制返回.默认10进制 注:如果参数为整数,则不做任何处理. 语法 intval (var, base) 参数 描述 var 必须.可以是任何标量类型. ...

  8. Matlab 之 字符串数组查找

    Matlab的优势在于向量操作,字符串操作往往费时费力,但是如果能充分利用Matlab自带的一些函数,也可以取得不错的效果.下面就介绍一下字符串数组查找的小技巧. 字符串数组我通常会选择应用cell格 ...

  9. token详解(转载)

    简介 在Web领域基于Token的身份验证随处可见.在大多数使用Web API的互联网公司中,tokens 是多用户下处理认证的最佳方式. 以下几点特性会让你在程序中使用基于Token的身份验证 1. ...

  10. Linux(五)__硬盘分区

    Linux中的文件管理机制是一种叫挂载和卸载的方式使用分区中的文件. 1.硬盘分区的概念 概述:首先我们要对硬盘分区的基本概念进行一些初步的了解,硬盘的分区主要分为基本分区(Primary Parti ...