lintcode-383-装最多水的容器
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-装最多水的容器的更多相关文章
- lintcode:装最多水的容器
装最多水的容器 给定 n 个非负整数 a1, a2, ..., an, 每个数代表了坐标中的一个点 (i, ai).画 n 条垂直线,使得 i 垂直线的两个端点分别为(i, ai)和(i, 0).找到 ...
- [LeetCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode] 11. Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- [LintCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- LeetCode第十一题-可以装最多水的容器
Container With Most Water 问题简介:通过一个给定数组,找出最大的矩形面积 问题详解:给定一个数组,包含n个非负整数a1,a2,…,an,其中每个表示坐标(i,ai)处的点,绘 ...
- 22.Container With Most Water(能装最多水的容器)
Level: Medium 题目描述: Given n non-negative integers a1, a2, ..., an , where each represents a point ...
- LeetCode:盛最多水的容器【11】
LeetCode:盛最多水的容器[11] 题目描述 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 ...
- Java实现 LeetCode 11 盛最多水的容器
11. 盛最多水的容器 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) ...
随机推荐
- Python + 百度Api 通过地址关键字获得格式化的地址信息
由于用户输入是千奇百怪的,除了格式语法不合要求之外的,即便是所谓的合法数据也是五花八门.尤其是地址,所有才由此文. 百度Api注册一个账号,创建一个应用后就会有一个`ak`的参数,就够了. Pytho ...
- tp5 的nginx配置
下面简单说明一下tp5运行在nginx上的配置. 原文地址:小时刻个人博客>http://small.aiweimeng.top/index.php/archives/tp5_nginx.htm ...
- 学在Java之前
java基础 下载JDK JDK(Java Development Kit Java开发工具包) 官方网址: www.oracle.com 参阅oracle.html ...
- js点击后将文字复制到剪贴板,将图片复制到画图
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD& ...
- 20155209实验二《Java面向对象程序设计》
20155209实验二<Java面向对象程序设计> 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计 ...
- 20155235 2006-2007-2 《Java程序设计》第1周学习总结
20155235 2006-2007-2 <Java程序设计>第1周学习总结 教材学习内容总结 第二章 使用的JRE不同,对JAVA的执行有什么影响 第三章 字符串的用法在JAVA和C中有 ...
- 20155302 2016-2017-2 《Java程序设计》第4周总结
20155302 2016-2017-2 <Java程序设计>第4周学习总结 教材学习内容总结 有关类的继承的理解:类实现继承的格式:class 子类名 extends 父类名 类的继承有 ...
- 20155323 2016-2017-2 《Java程序设计》第10周学习总结
20155323 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 狭义的网络编程范畴就是程序 ...
- 20155327李百乾 Exp3 免杀原理与实践
20155327李百乾 Exp3 免杀原理与实践 实践guocheng 一.Msfvenom使用编码器 1.利用(virustota)[https://www.virustotal.com/]检测实验 ...
- Storyboarding by Scripting
Storyboarding by Scripting In the .osu file, under [Events]:Note: underscores can be replaced with s ...