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.

Notice

You may not slant the container.

Have you met this question in a real interview?

Yes
Example

Given [1,3,2], the max area of the container is 2.

LeetCode上的原题,请参见我之前的博客Container With Most Water

解法一:

class Solution {
public:
/**
* @param heights: a vector of integers
* @return: an integer
*/
int maxArea(vector<int> &heights) {
int res = , i = , j = heights.size() - ;
while (i < j) {
res = max(res, min(heights[i], heights[j]) * (j - i));
heights[i] < heights[j] ? ++i : --j;
}
return res;
}
};

解法二:

class Solution {
public:
/**
* @param heights: a vector of integers
* @return: an integer
*/
int maxArea(vector<int> &heights) {
int res = , i = , j = heights.size() - ;
while (i < j) {
int h = min(heights[i], heights[j]);
res = max(res, h * (j - i));
while (i < j && h == heights[i]) ++i;
while (i < j && h == heights[j]) --j;
}
return res;
}
};

[LintCode] Container With Most Water 装最多水的容器的更多相关文章

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

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

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

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

  3. 【LeetCode】11. Container With Most Water 盛最多水的容器

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...

  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. 011 Container With Most Water 盛最多水的容器

    给定 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) .画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线,使得它们 ...

  6. Leetcode11.Container With Most Water盛最多水的容器

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...

  7. lintcode:装最多水的容器

    装最多水的容器 给定 n 个非负整数 a1, a2, ..., an, 每个数代表了坐标中的一个点 (i, ai).画 n 条垂直线,使得 i 垂直线的两个端点分别为(i, ai)和(i, 0).找到 ...

  8. LeetCode第十一题-可以装最多水的容器

    Container With Most Water 问题简介:通过一个给定数组,找出最大的矩形面积 问题详解:给定一个数组,包含n个非负整数a1,a2,…,an,其中每个表示坐标(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. Jquary入门( 修改内容)

    1. 使用JQ时需要先引用 JQ 包: 其他的JQ代码 需要写在 引用标签的下面如下图[基本格式] JQ中 是纯代码 没有判断 没有循环   如果 有 时间间隔和延迟  则使用JS 代码 详见 下面例 ...

  2. Yii 1开发日记 -- 后台搜索功能下拉及关联表搜索

    Yii 1 实现后台搜索,效果如下: 一. 下拉搜索: 1.模型中和常规的一样 if (isset($_GET['agency']['status']) && $_GET['agenc ...

  3. 【转】网络编程socket基本API详解

    转自:http://www.cnblogs.com/luxiaoxun/archive/2012/10/16/2725760.html socket socket是在应用层和传输层之间的一个抽象层,它 ...

  4. sql 如何将拼的sql语句结果赋值给变量

    SET @SQLS='select @AllUser=count(1) from (select PhoneId from '+@TABLE+' where SiteId='+convert(VARC ...

  5. iOS自动检测版本更新

    虽然苹果官方是不允许应用自动检测更新,提示用户下载,因为苹果会提示你有多少个软件需要更新,但是有的时候提示用户一下有新版还是很有必要的. 首先说一下原理: 每个上架的苹果应用程序,都会有一个应用程序的 ...

  6. .NET Attribute 入门【笔记】

    闲谈:没有系统的学过,偶尔看看,看的也不是很清楚. 却一直不明白,本来也不难,自己动手写了个示例.结果一目了然…… Attribute —— 标记 可以对方法.类等事务进行附着.增加属性. 下面以我见 ...

  7. POCO库——Foundation组件之加解密Crypt

    加解密Crypt:内部提供多种加解密方式.信息摘要提取.随机数产生等,具体的算法内部实现不做研究学习: DigestEngine.h :DigestEngine类作为各种摘要提取的基类,提供必要的接口 ...

  8. Centos 下编译安装Redis

    安装环境说明: 操作系统:Centos 6.5 -64bit 安装路径:/usr/local/redis 开始编译redis tar -zxvf redis-2.8.19.tar.gz cd redi ...

  9. linux中find批量删除空文件夹

    空文件夹 列出用find 删除管道即可 find -type d -empty | xargs -n 1 rm -rf 注意最后不能rm -f,这样删不了目录,必须-r

  10. 快速上手seajs——简单易用Seajs

    快速上手seajs——简单易用Seajs   原文  http://www.cnblogs.com/xjchenhao/p/4021775.html 主题 SeaJS 简易手册 http://yslo ...