11. Container With Most Water[M]盛最多水的容器
题目
Given \(n\) non-negative integers \(a_1,a_2,\cdots,a_n\), where each represents a point at coordinate\((i,a_i)\),\(n\) vetical lines are drawn such that the two endpoints of line \(i\) is at \((i,a_i)\) and \((i,0)\). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and \(n\) is at least 2.

#思路
思路1:暴力匹配法
考虑到每一种出现的线段组合,并求出这些组合下的最大面积。
思路2:双指针法
两线段的距离越远,面积越大;同时,线段之间形成的容器区域受到长度较短的那条的线段限制。
在数组中使用双指针,一个指向数组的开头,一个指向末尾。计算线段形成的区域面积,并用\(max\)存储目前为止获得的最大区域的面积。此外,指针的移动方向是,较短线段的指针向较长线段的指针移动。
由于矩形容器的面积受限于较短长度的线段,当较长线段的指针向较短线段移动时,矩形面积不会增加。但是,当移动较短线段的指针虽然会影响矩形的宽度的减小,但是由于移动的方向是将较短线段的指针向较长线段的指针移动,矩形会获得更大的高度,这可能会导致面积的增加
Tips
双指针法
利用两个指针来遍历数组,一般来说,遍历数组采用的是单指针。两个指针一般用于有序数组,利用两个相同方向或者相反方向的指针完成遍历。(这里的指针并非C中的指针,而是指数组索引)
C++
- 思路1
class Solution {
public:
int maxArea(vector<int>$ height){
int max = 0;
for(int i = 0; i< height.size(); i++){
for(int j = 0; j< height.size(); j++){
int wide = height[i] > height[j] ? height[j] : height[i];
int len = j - i;
int temMax = len * wide;
if(tempMax > max)
max = tempMax;
}
}
return max;
}
};
- 思路2
class Solution {
public:
int maxArea(vector<int>& height){
int pBegin = 0; //开始指针
int pEnd = height.size() - 1; //末尾指针
int max = 0;
while(pBegin < pEnd){
int minHeight = height[pBegin] < height[pEnd] ? height[pBegin] : height[pEnd];
int tempMax = minHeight * (pEnd - pBegin);
if (tempMax > max)
max = tempMax;
if (height[pBegin] < height[pEnd]){
pBegin ++;
}
else{
pEnd --;
}
}
return max;
}
};
Python
- 思路2
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
pBegin = 0
pEnd = len(height) - 1
resInt = 0
while pBegin < pEnd:
curArea = (pEnd - pBegin) *min(height[pBegin], height[pEnd])
if curArea > resInt:
resInt = curArea
if height[pBegin] < height[pEnd]:
pBegin += 1
else:
pEnd -= 1
return resInt
11. Container With Most Water[M]盛最多水的容器的更多相关文章
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 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) ...
- 力扣Leetcode 11. 盛最多水的容器
盛最多水的容器 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找 ...
- LeetCode---11. 盛最多水的容器(Java)
11. 盛最多水的容器 题目地址:https://leetcode-cn.com/problems/container-with-most-water/ 给你 n 个非负整数 a1,a2,...,an ...
- 【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). ...
- LeetCode 11. 盛最多水的容器(Container With Most Water)
题目描述 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两 ...
- [Swift]LeetCode11. 盛最多水的容器 | Container With Most Water
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
随机推荐
- canvas动画简单操作
canvas动画 小球滚动效果 关键api: window.requestAnimationFrame(draw) 会递归调用draw函数,替代setInterval var x = 20; var ...
- 让break跳出外层循环的方法
demo //在里层循环里面,想办法让外层循环的条件不成立,就可以控制外层循环结束. for(var i = 0 ; i < 10; i++){ alert(i) for(var j = 0 ; ...
- Fragment_动态加载
1.新建Fragment的XML布局文件. 2.在activity.xml中添加需要加载Fragment.列如: <?xml version="1.0" encoding=& ...
- hdu1385 Minimum Transport Cost 字典序最小的最短路径 Floyd
求最短路的算法最有名的是Dijkstra.所以一般拿到题目第一反应就是使用Dijkstra算法.但是此题要求的好几对起点和终点的最短路径.所以用Floyd是最好的选择.因为其他三种最短路的算法都是单源 ...
- 非阻塞方式connect编程
参考博客: ①setsockopt()函数使用详解:http://blog.csdn.net/tody_guo/article/details/5972588 ②setsockopt :SO_LING ...
- (转)基于MVC4+EasyUI的Web开发框架经验总结(7)--实现省份、城市、行政区三者联动
http://www.cnblogs.com/wuhuacong/p/3841338.html 为了提高客户体验和进行一些技术探索,现在正准备把我自己的客户关系管理系统CRM在做一个Web的版本,因此 ...
- The features of Swift
The features of Swift are designed to work together to create a language that is powerful, yet fun t ...
- 一系列令人敬畏的.NET核心库,工具,框架和软件
内容 一般 框架,库和工具 API 应用框架 应用模板 身份验证和授权 Blockchain 博特 构建自动化 捆绑和缩小 高速缓存 CMS 代码分析和指标 压缩 编译器,管道工和语言 加密 数据库 ...
- MongoDB 学习笔记(四):索引
一.索引的基本使用 1.建立索引 在shell中为某个key建立索引的方法为:db.集合名.ensureIndex({key:1}),其中的key表示为哪个key建立索引,1表示升序建立索引,而-1表 ...
- 【转】【Oracle 集群】Oracle 11G RAC教程之集群安装(七)
原文地址:http://www.cnblogs.com/baiboy/p/orc7.html 阅读目录 目录 集群安装 参考文献 相关文章 Oracle 11G RAC集群安装(七) 概述:写下本文档 ...