给定 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盛最多水的容器的更多相关文章

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

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

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

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

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

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

  7. LeetCode---11. 盛最多水的容器(Java)

    11. 盛最多水的容器 题目地址:https://leetcode-cn.com/problems/container-with-most-water/ 给你 n 个非负整数 a1,a2,...,an ...

  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. js 实现页面局部(或图片)放大功能(vue)

    方法: adjustStart1 (e) { e.preventDefault() let event = e.touches if (event.length === 2) { this.style ...

  2. day 42 02--CSS的继承性和层叠性

    02--CSS的继承性和层叠性   本节目录 一 继承性 二 层叠性 三 层叠性权重相同处理 一 继承性 css有两大特性:继承性和层叠性 面向对象语言都会存在继承的概念,在面向对象语言中,继承的特点 ...

  3. matlab-变量类型-数组-矩阵

    1 变量类型 1.1基本 1.2 特殊变量 ans •i,j: complex number •Inf: 无穷大 •eps: 2.2204e-016 •NaN: not a number •pi:pa ...

  4. HBase的安装与配置

  5. Ionic 发布Release 版本

    1.生成releases 版本  cordova build android --release 如果你在生成release版本出错,请修改build.gradle 2.生成签名文件 生成成功后会在G ...

  6. light oj 1071 dp(吃金币升级版)

    #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...

  7. 移动端网站如何开发(电脑端网站到手机端网站我们需要在html代码中添加哪个meta标签)

    移动端网站如何开发(电脑端网站到手机端网站我们需要在html代码中添加哪个meta标签) 一.总结 一句话总结: 添加viewport标签:meta name="viewport" ...

  8. 使用Tomcat过程中的常见问题

    1.点击startup.bat,启动Tomcat     DOS弹窗一闪而过 鼠标选中startup.bat这个文件,右键选择“编辑“,在末尾添加        pause

  9. Mycat+MySQL 主从复制

    一.主从复制搭建(新环境5.6.33)1.设置复制Master配置信息 [mysqld] #repl master库 server \logbin\mysql-bin max_binlog_size= ...

  10. docker 安装redis 并配置外网可以访问 - flymoringbird的博客 - CSDN博客

    原文:docker 安装redis 并配置外网可以访问 - flymoringbird的博客 - CSDN博客 端口映射,data目录映射,配置文件映射(在当前目录下进行启动). docker run ...