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.

Note: You may not slant the container.

这题O(n^2)的解法就是基本的暴力法。但是要想出O(n)的解法并不容易(实际上我就想不出来T_T),面试碰到这样难度的新题估计就跪了。

虽然有点悲观,但是解题的思路还是有迹可循的。

container的面积由两个因素组成,一个是高度,一个是宽度。

我们同样采用的是“按序枚举”的思路,高度是不确定的变量,不好枚举,但是宽度则是由1到n-1的确定的量,可以很方便的枚举。

此外,如同上题Candy,这种按序枚举的思路也可以是从左到右,从右往左等。

在想到对宽度进行枚举后,这种枚举有两种,一个是从小往大,另一个是从大往小。

如果我们想从小往大枚举,这种一般必须有dp的规律才能够使解法更优,但是从本题来看,每一个解都只与左右两个index有关,并不包含子问题。所以从小往大的枚举不可行。

那么从大往小枚举,比如3,2,5,4,1。最大的宽度,就是从3到1,这时我们可以算出一个面积。当我们缩小这个宽度,有两种方法,但是实际上只有右区间缩小一个可能得到最优解。为什么呢?因为每次对于左右边界中较小的那一个,当前的宽度都是最宽的,也就是它能达到的最大面积了。

由此我们可以只往一个方向进行左右边界的缩小,最终得到的方法是O(n)的。

Code:

class Solution {
public:
int maxArea(vector<int> &height) {
int n = height.size();
if(n < 2) return 0;
int res = 0; int left = 0, right = n-1;
while(left < right)
{
int tmp = min(height[left], height[right]) * (right - left);
if(tmp > res) res = tmp; if(height[left] < height[right]) left++;
else right--;
}
return res;
}
};

  

7 Container With Most Water_Leetcode的更多相关文章

  1. 在docker中运行ASP.NET Core Web API应用程序(附AWS Windows Server 2016 widt Container实战案例)

    环境准备 1.亚马逊EC2 Windows Server 2016 with Container 2.Visual Studio 2015 Enterprise(Profresianal要装Updat ...

  2. .Container与.container_fluid区别

    .Container与.container_fluid是bootstrap中的两种不同类型的外层容器,两者的区别是:.container 类用于固定宽度并支持响应式布局的容器..container-f ...

  3. View and Data API Tips: Constrain Viewer Within a div Container

    By Daniel Du When working with View and Data API, you probably want to contain viewer into a <div ...

  4. [LeetCode] 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

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

  6. Docker - command in docker container

    1.查看Container 里面运行的进程 在运行容器以后,可以查看里面的进程: docker top <container_id> or <container_name> 2 ...

  7. 【leetcode】Container With Most Water

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

  8. [LintCode] Container With Most Water 装最多水的容器

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

  9. ASP.NET MVC中使用Unity Ioc Container

    写在前面 安装Unity 添加服务层 IArticleRepository类型映射 服务注入到控制器 Global.asax初始化 后记 关于Unity的使用可以参照<Unity依赖注入使用详解 ...

随机推荐

  1. matlab下二重积分的蒙特卡洛算法

    %%monte_carlo_ff.m %被积函数(二重) function ff=monte_carlo_ff(x,y) ff=x*y^2;%函数定义处 end %%monte_carlo.m %蒙特 ...

  2. 无法对 索引 'IndexName' 执行 删除,因为它不存在,或者您没有所需的权限。

    先写结论: 语法:  DROP INDEX 表名.索引名 如果索引明明存在..却报标题上那个错..请直接去看是否是表名与库中的不一样.. 请一定去检查一下..别问我为什么这么说.. if exists ...

  3. RabbitMQ学习

    参考链接:http://www.cnblogs.com/leocook/p/mq_rabbitmq_0.html

  4. 海拔高度图*.dem文件的读取—vtkDEMReader

    vtkDEMReader reads digital elevation files and creates image data. Digital elevation files are produ ...

  5. mysql 快速简单安装法

    网上下载的编译好的包 最好安装在 /usr/local 目录下面: 我用的mysql的版本的是:mysql--linux-i686-icc-glibc23.tar.gz 在官网上就可以下载到. 先期工 ...

  6. SDC Tcl package of Timequest

    Tcl comand Tcl Commands all_clocks all_inputs all_outputs all_registers create_clock create_generate ...

  7. 读取图像,LUT以及计算耗时

    使用LUT(lookup table)检索表的方法,提高color reduce时对像素读取的速度. 实现对Mat对象中数据的读取,并计算color reduce的速度. 方法一:使用Mat的ptr( ...

  8. http状态代码-转载

    一些常见的状态码为: 200 – 服务器成功返回网页 404 – 请求的网页不存在 503 – 服务不可用 1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态代码. 代码 说明 100 ( ...

  9. Sublime text 3如何编辑less并转(编译)成css文件

    今天开始学习使用less这个强大方便的前端工具,本来是考虑用koala(专门编辑less的软件)来使用less的,但是发现sublime编辑器也可以实现对less的编译及高亮显示代码,这样既能少用一个 ...

  10. Visual Studio 默认保存为UTF8编码

    Visual Studio (中文版)默认保存的文本文件是GB2312编码(代码页936)的,默认的行尾(End of line)是CRLF的. 如果仅仅是在windows下开发问题也不大,但是涉及到 ...