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) ...
随机推荐
- eclipse快捷大全
Eclipse常用快捷键 1几个最重要的快捷键 代码助手:Ctrl+Space(简体中文操作系统是Alt+/)快速修正:Ctrl+1单词补全:Alt+/打开外部Java文档:Shift+F2 显示搜索 ...
- 2018-8-10-docfx-做一个和微软一样的文档平台
title author date CreateTime categories docfx 做一个和微软一样的文档平台 lindexi 2018-08-10 19:16:51 +0800 2018-2 ...
- 系统负载load
一.查看系统负荷 如果你的电脑很慢,你或许想查看一下,它的工作量是否太大了. 在Linux系统中,我们一般使用uptime命令查看(w命令和top命令也行).(另外,它们在苹果公司的Mac电脑上也适用 ...
- odoo:Actions
actions定义了系统对于用户的操作的响应:登录.按钮.选择项目等. 一:窗口action(ir.actions.act_window ) 最常用的action类型,用于将model的数据展示出来. ...
- day66作业
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 2018.10.29安装tensorflow
先安装tensorflow时按照中文社区安装,结果安装的0.5版本与cuda和cudnn版本不一样,后面才知道需要安好对应版本安装. 1.卸载protobuf pip uninstall protob ...
- memcpy 和 memmove
memcpy 原形为: void *memcpy(void *dest, const void *src, size_t n); 其用于内存空间的拷贝,但是并没有考虑内存重叠问题. memmove原形 ...
- dubbo入门学习(三)-----dubbo整合springboot
springboot节省了大量的精力去配置各种bean,因此通过一个简单的demo来整合springboot与dubbo 一.创建boot-user-service-provider 本篇博文基于上篇 ...
- Python-Jsonpath简单入门
原文来自:http://goessner.net/articles/JsonPath/ JSONPath - 是xpath在json的应用. xml最大的优点就有大量的工具可以分析,转换,和选择性 ...
- web api中允许跨域访问
①添加owin的引用 ②添加owin.Cors的引用 ③在WebApiConfig中添加 config.EnableCors(new EnableCorsAttribute("*" ...