【LeetCode】11. Container With Most Water 盛最多水的容器
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
- 个人公众号:负雪明烛
- 本文关键词:盛水,容器,题解,leetcode, 力扣,python, c++, java
题目地址:https://leetcode.com/problems/container-with-most-water/description/
题目描述
Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) 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.

The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
Example:
Input: [1,8,6,2,5,4,8,3,7]
Output: 49
题目大意
有很多挡板,从这些挡板中选两个,然后计算能够成的面积的最大值。
解题方法
双指针
如果纯暴力计算两两之间的面积,时间复杂度是O(N^2),肯定会超时。
一个比较好的解决的方法是,使用双指针方法,一个从最左边开始,一个从最右边开始,计算两个挡板之间的面积,然后在向中间移动。移动的规则是这样的,如果哪个挡板比较矮,就舍弃掉这个挡板,把指向这个挡板的指针向中间移动。
这样的移动方式是我们每次都保留了比较长的哪个挡板,也就能获得更多的水。当两个挡板的高度一样的话,移动任意一个即可,因为这两个是高度一样的挡板,如果中间有更高的挡板,那么当前的挡板决定了以后的挡板的最低值,也就是说以其中任意一个为边界的容器面积不可能超过当前的当前的值。
我们在遍历的过程中需要保留遍历时候得到的最大值,最后返回即可。
时间复杂度是O(N),空间复杂度是O(1).
代码如下:
class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
ans = 0
l = 0
r = len(height) - 1
while l < r:
ans = max(ans, min(height[l], height[r]) * (r - l))
if height[l] < height[r]:
l += 1
else:
r -= 1
return ans
参考资料:
https://www.youtube.com/watch?v=IONgE6QZgGI
https://leetcode.com/articles/container-with-most-water/
https://leetcode.windliang.cc/leetCode-11-Container-With-Most-Water.html
日期
2018 年 9 月 23 日 —— 今天是实验室第一个打卡的
【LeetCode】11. Container With Most Water 盛最多水的容器的更多相关文章
- [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 装最多水的容器
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).找出其中的两条线,使得它们 ...
- Leetcode11.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). ...
- [LintCode] 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. [👁] Container With Most Water & two pointers
盛最多水的容器 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找 ...
随机推荐
- 【R读取报错】解决: Can't bind data because some arguments have the same name
最近读取一个数据时,报如标题的错误. args[1] <- "RT_10-VS-RT_0" all <- read.delim(paste0(args[1]," ...
- MacBookpro安装VMware Fusion虚拟机,并安装win7 64位系统
1.准备好安装用的东西(准备好正确的东西,安装路上就成功了一半)(1)VMware Fusion 附带注册机生成注册码,链接: https://pan.baidu.com/s/13Qm9zPOFjFt ...
- Excel-返回列表或数据库中的分类汇总(汇总可以实现要还是不要统计隐藏行功能) subtotal()
SUBTOTAL函数 函数名称:SUBTOTAL 主要功能:返回列表或数据库中的分类汇总. 使用格式:SUBTOTAL(function_num, ref1, ref2, ...) 参数说明:Func ...
- 使用Redis实现令牌桶算法
在限流算法中有一种令牌桶算法,该算法可以应对短暂的突发流量,这对于现实环境中流量不怎么均匀的情况特别有用,不会频繁的触发限流,对调用方比较友好. 例如,当前限制10qps,大多数情况下不会超过此数量, ...
- Hive(九)【自定义函数】
目录 自定义函数 编程步骤 案例 需求 1.创建工程 2.导入依赖 3.创建类 4.打jar包 5.上传hive所在服务器 6.将jar添加到hive的classpath 7.创建临时函数与开发好的j ...
- C++字节对齐(对象大小)
内部数据成员对齐参考这篇 https://www.cnblogs.com/area-h-p/p/10316128.html 这里只强调C++字节对齐特点 ①静态数据成员属于类域,在对象中不占大小 ②若 ...
- android studio 使用 aidl(三)权限验证
这篇文章是基于android studio 使用 aidl (一) 和 android studio 使用 aidl(二) 异步回调 下面的代码都是简化的,如果看不懂请先移步上2篇文章 网上的东西太坑 ...
- Mybatis 批量插入
一.首先对于批量数据的插入有两种解决方案(下面内容只讨论和Mysql交互的情况) 1)for循环调用Dao中的单条插入方法 2)传一个List<Object>参数,使用Mybatis的批量 ...
- Android 清除本地缓存
主要功能:清除内.外缓存,清除数据库,清除Sharepreference,清除files和清除自定义目录 public class DataCleanManager { //清除本应用内部缓存(/da ...
- CentOS 初体验三: Yum 安装、卸载软件
一:Yum 简介 Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指 ...