题目

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

  1. LeetCode 11. Container With Most Water (装最多水的容器)

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

  2. LeetCode:盛最多水的容器【11】

    LeetCode:盛最多水的容器[11] 题目描述 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为  ...

  3. Java实现 LeetCode 11 盛最多水的容器

    11. 盛最多水的容器 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) ...

  4. 力扣Leetcode 11. 盛最多水的容器

    盛最多水的容器 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找 ...

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

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

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

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

  7. [LeetCode]11. Container With Most Water 盛最多水的容器

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

  8. LeetCode 11. 盛最多水的容器(Container With Most Water)

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

  9. [Swift]LeetCode11. 盛最多水的容器 | Container With Most Water

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

随机推荐

  1. Codeforces Round #455

    Generate Login 第二个单词肯定只取首字母 Solution Segments 从1开始的线段和在n结束的线段各自凑一凑,剩下的转化为规模为n-2的子问题. Solution Python ...

  2. Java基础4一数组

    数组 数组是用来存储一组相同类型数据的数据结构 数组变量属于引用数据类型 数组中的元素可以是任何数据类型(基本类型和引用类型) 备注:数组中存放的都是相同数据类型的数据. 1.一维数组的声明 语法:数 ...

  3. 第五课: - Stack / Unstack / Transpose函数

    第 5 课   我们将简要介绍 stack 和 unstack 以及 T (Transpose)函数. 在用pandas进行数据重排时,经常用到stack和unstack两个函数.stack的意思是堆 ...

  4. 利用JavaScript的if语句判断元素显示隐藏

    <html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...

  5. 了解权限控制框架shiro 之实际应用.

    Apache Shiro 1.权限控制分为 a.粗粒度 URL 级别权限控制     b.细粒度方法级别权限控制 2.使用shiro进行权限控制主要有四种主要方式 : a. 在程序中 通过 Subje ...

  6. bootstrap modal 一闪

    原因可能是因为bootstrap.min.js(bootstrap.js) 和modal.js重复引用导致的,而且重复引用还会引致bootstrap的js事件失效.

  7. java中后端拼接字符串返回前台页面换行显示

    后端拼接时用:"\n"分割,比如: String str = "白日依山尽,\n" + "黄河入海流:"; 返回前台页面时,放入 <p ...

  8. Nginx负载均衡health_check分析

    在Nginx负载均衡中,我们很难保证说每一台应用服务器都能一直正常的运行下去.但是我们可以通过设置Nginx来检测这些应用服务器,检测这些服务器当中不能访问的. Nginx的检测方式分为两种,一种是被 ...

  9. 更新时间戳.txt

    UPDATE bbs2 INNER JOIN time1 ON bbs2.AnnounceID = time1.AnnounceID SET bbs2.asptime = time1.asptime

  10. CF140E New Year Garland (计数问题)

    用$m$种颜色的彩球装点$n$层的圣诞树.圣诞树的第$i$层恰由$a_{i}$个彩球串成一行,且同一层内的相邻彩球颜色不同,同时相邻两层所使用彩球的颜色集合不 同.求有多少种装点方案,答案对$p$取模 ...