Leetcode11.Container With Most Water盛最多水的容器
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
说明:你不能倾斜容器,且 n 的值至少为 2。

示例:
输入: [1,8,6,2,5,4,8,3,7] 输出: 49
方法一:暴力法
class Solution {
public:
int maxArea(vector<int>& height)
{
int len = height.size();
int MAX = 0;
for(int i = 0; i < len; i++)
{
for(int j = i + 1; j < len; j++)
{
MAX = max(MAX, (j - i) * min(height[i], height[j]));
}
}
return MAX;
}
};
方法二双指针:
双指针:为了使面积最大化,我们需要考虑更长的两条线段之间的区域。如果我们试图将指向较长线段的指针向内侧移动,矩形区域的面积将受限于较短的线段而不会获得任何增加。但是,在同样的条件下,移动指向较短线段的指针尽管造成了矩形宽度的减小,但却可能会有助于面积的增大。
class Solution {
public:
int maxArea(vector<int>& height)
{
int MAX = 0;
int low = 0;
int high = height.size() - 1;
while(low < high)
{
MAX = max(MAX, (high - low) * min(height[low], height[high]));
if(height[low] < height[high])
{
low++;
}
else
{
high--;
}
}
return MAX;
}
};
Leetcode11.Container With Most Water盛最多水的容器的更多相关文章
- 【LeetCode】11. Container With Most Water 盛最多水的容器
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...
- [LeetCode]11. Container With Most Water 盛最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- 011 Container With Most Water 盛最多水的容器
给定 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. 盛最多水的容器(Java)
11. 盛最多水的容器 题目地址:https://leetcode-cn.com/problems/container-with-most-water/ 给你 n 个非负整数 a1,a2,...,an ...
- 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) ...
随机推荐
- php链表笔记:合并两个有序链表
<?php /** * Created by PhpStorm. * User: huizhou * Date: 2018/12/2 * Time: 15:29 */ /** * 合并两个有序链 ...
- Java-MyBatis-MyBatis3-XML映射文件:结果映射
ylbtech-Java-MyBatis-MyBatis3-XML映射文件:结果映射 1.返回顶部 1. 结果映射 resultMap 元素是 MyBatis 中最重要最强大的元素.它可以让你从 90 ...
- Spring MVC(七)--传递JSON参数
有时候参数的传递还需要更多的参数,比如一个获取用户信息的请求中既有用户ID等基本参数,还要求对查询结果进行分页,针对这种场景,一般都会将分页参数封装成一个对象,然后将它和基本参数一起传给控制器,为了控 ...
- https证书加密
对称加密 浏览器向服务端发送请求时,服务端首先给浏览器发送一个秘钥,浏览器用秘钥对传输的数据进行加密后发送给浏览器,浏览器拿到加密后的数据使用秘钥进行解密 非对称加密 服务端通过rsa算法生成一个公钥 ...
- 「loj#6261」一个人的高三楼
题目 显然存在一个这样的柿子 \[S^{(k)}_i=\sum_{j=1}^iS^{(k-1)}_j\] 我们可以视为\(S^{(k)}\)就是由\(S^{(k-1)}\)卷上一个长度为\(n\)全是 ...
- python-web-selenium模拟控制浏览器
用 selenium 模块控制浏览器 启动 selenium 控制的浏览器 from selenium import webdriver brower = webdriver.Firefox() br ...
- [转]WPF--模板选择
典型的,把模板关联到一块特定的数据上,不过通常希望动态的确定使用哪个模板---既可以基于一个属性值,也可以是一个全局状态.当真正需要大规模替换模板时,也可以使用DataTemplateSelector ...
- 关于jQuery中attr(),prop()的使用
注意:什么时候使用attr(),什么时候使用prop()?1.添加属性名称该属性就会生效应该使用prop();2.是有true,false两个属性使用prop();3.其他则使用attr(); 以下是 ...
- HZOI20190903模拟36 字符,蛋糕,游戏
题面:https://www.cnblogs.com/Juve/articles/11461528.html A:字符 暴力模拟一下,细节很多,但是暴力思路都不大一样 先枚举循环节长度,然后处理一个b ...
- NVIDIA驱动安装、CUDA安装、cudnn安装
1.禁用 nouveau 驱动 sudo vim /etc/modprobe.d/nvidia-installer-disable-nouveau.conf 或者 sudo vim /etc/modp ...