Given n non-negative integers a1 a2 , ..., an , where each represents a point at coordinate (iai ). n vertical lines are drawn such that the two endpoints of line i is at (iai ) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container.

题意:数组的值为容器的高度,下标之差为容器的宽度,求最大面积

思路:这题其实和3sum closest类似,从数组的两端,向中间遍历,计算最大面积,然后将高度较小的那端向前递进一个就行。如:3 5 8 7 6 2;开始时,面积为2*(5-0)=10;然后,尾端前进一个单位,面积为3*(4-0)=12;依次类推,取面积的最大值即可。时间的复杂度为O(n)代码如下:

 class Solution {
public:
int maxArea(vector<int> &height)
{
int l=,r=height.size()-;
int mArea=;
while(l<r)
{
mArea=max(mArea,(r-l)*min(height[l],height[r]));
height[l]<height[r]?l++:r--;
}
return mArea;
}
};

还有一种常规的思路:就是使用两个指针,一个固定,另一个从第一个的后面一个开始不断的向后遍历,求最大的面积;然后 移动第一个,第二重复上述操作。这种思路用两个for循环。但时间的复杂度为O(n^2)。

[Leetcode] container with most water 最大水容器的更多相关文章

  1. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

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

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

  3. [LeetCode]Container With Most Water, 解题报告

    前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n no ...

  4. C++LeetCode:: Container With Most Water

    本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做.感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过.每天都要被LeetCo ...

  5. leetcode Container With Most Water

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

  6. [LeetCode] Container With Most Water 简要分析

    前言 这题非要说贪心的话也算是吧,不过最主要的特征还是双指针.LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的.这道题倒是“短板效应”的不错体现了. 题目 题目链接 Given n non-neg ...

  7. [Leetcode] Container With Most Water ( C++)

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

  8. LeetCode——Container With Most Water

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

  9. LeetCode Container With Most Water (Two Pointers)

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

随机推荐

  1. Angular : 响应式编程, 组件间通信, 表单

    Angular 响应式编程相关 ------------------------------------------------------------------------------------ ...

  2. PHP计算翻页

    function fanye() { if ($total <= $num) { $list['curTotal'] = $total; } else { $offsetA = $start; ...

  3. mysql中tinyint、smallint、mediumint,int 和bigint 的区别

    1:字节的定义:字节(Byte):字节是通过网络传输信息(或在硬盘或内存中存储信息)的单位. 一个英文字母(不分大小写)占一个字节的空间,一个中文汉字占两个字节的空间 2.在mysql中一个汉字占三个 ...

  4. 使用SQLite删除Mac OS X 中launchpad里的快捷方式

    一般情况下,从App Store安装的应用程序,如果应用删除,那么launchpad里对应的图标会一起删除了. 而对于不是通过App Store安装的应用程序,删除应用程序,Launchpad中很可能 ...

  5. java时间"yyyy-mm-dd HH:mm:ss"转成Date

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time="1 ...

  6. python2.7入门---元组

        这次我们来学习下python中的元组.首先,基础认知点是,Python的元组与列表类似,不同之处在于元组的元素不能修改.元组使用小括号,列表使用方括号.元组创建很简单,只需要在括号中添加元素, ...

  7. spark优化系列一:参数介绍

    1 spark on yarn常用属性介绍 属性名 默认值 属性说明 spark.yarn.am.memory 512m 在客户端模式(client mode)下,yarn应用master使用的内存数 ...

  8. Hibernate-ORM:12.Hibernate中的多对多关联关系

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客将讲述Hibernate中的多对多关联关系的操作,准备的篇幅较少,望海涵 一,讲述多对多 多对多的关联 ...

  9. JS控制文本框输入的内容

    总而言之:   先在‘<input>’ 里输入      onkeyup="value=value.replace(/[^\X]/g,'')" 然后在(/[\X]/g, ...

  10. Anytime项目开发记录0

    Anytime,中文名:我很忙. 开发者:孤独的猫咪神. 这个项目会持续更新,直到我决定不再维护这个APP. 2014年3月10日:近日有事,暂时断更.希望可以会尽快完事. 2014年3月27日:很抱 ...