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) ...
随机推荐
- PKU_3624(0-1背包)
题目:http://poj.org/problem?id=3624 分析:这是一个0-1背包的问题. #include<stdio.h>#include<string.h>in ...
- python验证码识别PIL+pytesseract
1.需要模块安装 在python安装目录scripts即: 执行pip install pillow 下载tesseract-ocr-setup-4.00.00dev.exe 安装,我的目录在C盘默认 ...
- Extjs 疑难杂症 (LoadMark 遮罩、Panel Update无效、chrome浏览器date控件全屏)
一.在extjs gridPanel中使用LoadMark无效,三步搞定. 原代码: grid = new Ext.grid.GridPanel({ store: store, title:'资料列表 ...
- tmux连接时多个显示器分别显示不同的窗口大小
如果两个电脑连接同一个tmux,但是他们各自的显示器大小不同,那么就会在一个显示器部分会显示灰色区.在tmux里面有个设置可以更改,在tmux里面输入命令 tmux set-window-opti ...
- CentOS7.4 安装JDK 步骤
1.先在官网下载jdk1.8的压缩文件 2.用putty将压缩文件拷到home目录下 3.新建一个/home/jdk1.8目录 : mkdir /home/jdk1.8 4.将压缩文件解压到hom ...
- LeetCode简单算法之分割平衡字符串 #1221
在一个「平衡字符串」中,'L' 和 'R' 字符的数量是相同的. 给出一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串. 返回可以通过分割得到的平衡字符串的最大数量. 示例 1: 输入:s = ...
- MATLAB---dir函数
dir函数是最常用的转换路径的函数,可以获得指定文件夹下的所有子文件夹和文件,并存放在一个文件结构的数组中,这个数组各结构体内容如下: name -- 文件名 date -- 修改日期 b ...
- 901. Online Stock Span [短于线性的时间统计单个元素的Span ]
Span 指这个元素之前连续的小于这个元素的值有多少个 原理: 维护递减栈 这个栈内的元素是递减的序列 新到一个元素x 依次出栈比x小的(也就是这个元素的Span) 这种问题的关键在于 新来的元素如果 ...
- 嘴巴题2 UVA10779 收集者的难题
UVA10779 收集者的难题 题目: 有\(T(T\leq 20)\)组数据.\(Bob\)在与他的\(n−1(2\leq n\leq 10)个\)朋友交换糖纸,一共有\(m(5\leq m\leq ...
- UVA11722 Jonining with Friend
Joining with Friend You are going from Dhaka to Chittagong by train and you came to know one of your ...