383-装最多水的容器

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

注意事项

容器不可倾斜。

样例

给出[1,3,2], 最大的储水面积是2.

标签

两根指针 数组

思路

从左右两边向中间逼近,若要使面积增大,则必须找到更大的容器高度(因为容器长度在变短),所以保留长的那条线段,使得短线段向另一方逐渐逼近

code

class Solution {
public:
/*
* @param : a vector of integers
* @return: an integer
*/
int maxArea(vector<int> heights) {
// write your code here
int size = heights.size();
if (size <= 0) {
return 0;
} int result = 0, left = 0, right = size-1;
while (left < right) {
result = max(result, min(heights[left], heights[right])*(right - left));
if (heights[left] < heights[right]) {
left++;
}
else {
right--;
}
}
return result;
}
};

lintcode-383-装最多水的容器的更多相关文章

  1. lintcode:装最多水的容器

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

  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] 11. Container With Most Water 装最多水的容器

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

  4. [LintCode] 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. LeetCode第十一题-可以装最多水的容器

    Container With Most Water 问题简介:通过一个给定数组,找出最大的矩形面积 问题详解:给定一个数组,包含n个非负整数a1,a2,…,an,其中每个表示坐标(i,ai)处的点,绘 ...

  7. 22.Container With Most Water(能装最多水的容器)

    Level:   Medium 题目描述: Given n non-negative integers a1, a2, ..., an , where each represents a point ...

  8. LeetCode:盛最多水的容器【11】

    LeetCode:盛最多水的容器[11] 题目描述 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为  ...

  9. Java实现 LeetCode 11 盛最多水的容器

    11. 盛最多水的容器 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) ...

随机推荐

  1. Fiddler抓取手机APP数据包

    第一步:下载神器Fiddler,下载链接: http://w.x.baidu.com/alading/anquan_soft_down_ub/10963 下载完成之后,傻瓜式的安装一下了! 第二步:设 ...

  2. Google protobuf使用技巧和经验

    Google protobuf是非常出色的开源工具,在项目中可以用它来作为服务间数据交互的接口,例如rpc服务.数据文件传输等.protobuf为proto文件中定义的对象提供了标准的序列化和反序列化 ...

  3. SQL学习笔记:函数

    SQL函数 AVG select AVG(col) AS avgvalue from tablename select col2 from tablename where col1>(selec ...

  4. 学号20155308 2006-2007-2 《Java程序设计》第3周学习总结

    学号20155308 2006-2007-2 <Java程序设计>第3周学习总结 教材学习内容总结 对象(Object):存在的具体实体,具有明确的状态和行为 类(Class):具有相同属 ...

  5. 20155325 2016-2017-2 《Java程序设计》课程总结

    (按顺序)每周作业链接汇总 预备作业1:浅谈对师生关系的看法以及对未来学习生活的展望 预备作业2:学习娄老师<做中学>系列文章.自身C语言情况.Java课程目标 预备作业3:安装虚拟机情况 ...

  6. Oracle下如何用rman备份到特定的sequence

    本文为摘抄,目的为方便日后阅读: http://docs.oracle.com/cd/B12037_01/server.101/b10734/rcmbackp.htm To determine the ...

  7. zabbix最新版3.4搭建(根据官方文档适当修改)

    操作系统:CentOS Linux release 7.4.1708 (Core) 1.安装apache 1.1 安装apache yum install httpd httpd-devel 1.2 ...

  8. superset 安装测试,基于windows 和 centos7.x

    1.刚开始在windows平台测试搭建,报各种问题,搭建可以参考官网https://superset.incubator.apache.org/installation.html#deeper-sql ...

  9. php-laravel安装与使用

    1.框架作用    提供了一些主体功能,方便开发者快速开发 2.PHP框架    laravel    ThinkPHP 3.首先要安装composer软件    1.作用        主要管理PH ...

  10. bilibili携手WeTest,保障视频类应用优质适配体验

    WeTest 导读 中国移动视频用户规模越来越大,各类移动视频APP也百家争鸣, B站作为国内知名的年轻人文化社区,bilibili在推出移动端时,除了坚持自身的独特定位以外,对其APP的质量也十分重 ...