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. android 按钮动态点击

    关键代码: 1.创建一个btn_selector.xml的文件 <?xml version="1.0" encoding="utf-8"?>< ...

  2. flask的模板

    flask用的是jinja2的模板 模板其实是一个包含响应文本的文件,其中用占位符(变量)表示动态部分,告诉模板引擎其具体的值需要从使用的数据中获取 使用真实值替换变量,再返回最终得到的字符串,这个过 ...

  3. Python学习:1.快速搭建python环境

    一.安装python 现在python有两个比较大的版本一个是python3.x一个是python2.x,python3.x相当于与python2.x是一个比较大的升级,但是python3.x没有向下 ...

  4. ctf题目writeup(7)

    2019.2.10 过年休息归来,继续做题. bugku的web题,地址:https://ctf.bugku.com/challenges 1. http://123.206.87.240:8002/ ...

  5. C语言结构体的学习,以及gdb的调式

    #include <stdio.h> #include <string.h> #define format "%d\n%s\n%f\n%f\n%f\n" t ...

  6. LINUX系统配置相关

    修改系统引导文件 grub.cfg的文件位置   /boot/grub/grub.cfg set default="4"  默认windows是在第四个选项 set timeout ...

  7. http报文和浏览器缓存机制

    目录 1. 请求报文 1.1请求行 1.2 请求头 一些常用的请求头信息 2. 响应报文 2.1 状态行 1> 响应状态码 2> 常见的响应状态码 2.2 响应头 3. 浏览器缓存 3.1 ...

  8. Python的文件输入输出,如何追加内容,读取内容,添加内容

    python主要的文件打开的几种访问模式 模式可以为读模式('r').写模式('w')或追加模式('a'),当然还有rb.wb.ab.r+.w+.a+.rb+.wb+.ab+,然而实际从代码上我也没看 ...

  9. 排查实时tail功能cpu占用过高问题

    “你的python应用cpu占用快90%了!!!”,良哥朝我眨了眨布满血丝的眼睛“不会吧”,我心想:我这是好的啊 没接触过kafka的同学可以先了解下:([http://www.jasongj.com ...

  10. [USACO18DEC]Fine Dining

    题面 \(Solution:\) 一开始想的是先跑一遍最短路,然后拆点之后再跑一遍,比较两次dis,然后发现拆点后会有负环(可能是我没想对拆点的方法),于是就放弃了拆点法. 我们考虑强制让每头牛选择走 ...