【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).找 ...
随机推荐
- 利用plink软件基于LD信息过滤SNP
最近有需求,对WGS测序获得SNP信息进行筛减,可问题是测序个体少,call rate,maf,hwe,等条件过滤后,snp数量还是千万级别,所以后面利用plink工具根据LD信息来滤除大量SNP标记 ...
- Golang gRPC调试工具
目录 Golang gRPC调试工具 1. 命令行工具 grpcurl 1.1 安装 1.2 验证 1.3 注册反射 1.4 使用示例 2. web调试工具grpcui 2.1 安装 2.2 验证 2 ...
- Excel—分组然后取每组中对应时间列值最大的或者最小的
1.MAX(IF(A:A=D2,B:B)) 输入函数公式后,按Ctrl+Shift+Enter键使函数公式成为数组函数公式. Ctrl+Shift+Enter: 按住Ctrl键不放,继续按Shift键 ...
- 日常Java 2021/10/5
java 异常处理 Throwable中包括Error 和Exception,Exception包括IOException和RuntimeException 抛出异常 1.异常运算条件 Arithme ...
- github小白的记录随笔
此文章是基础本地安装好了git环境的新手小白. 进入您要上传项目的根路径,右键选择Git Bash Here. 输入命令: git init //初始化git仓库环境 git remote add o ...
- D3学习-加载本地数据
在加载本地数据时,弄了很久都无法显示出来,后来才知道是要把数据文件和html文件都加载到服务器上面 这样就可以显示出来了,
- IOS_UIButton去掉系统的按下高亮置灰效果
setAdjustsImageWhenHighlighted // default is YES. if YES, image is drawn darker when highlighted(p ...
- 删除button中除label之外的View
因为button中的UIButtonLabel判断class类型时,会被认为是view,所以想删除view类型的子控件时,会将label也删掉,从而无法显示title,此时,可以给指定的View添加t ...
- FastJson简介
FastJson简介 首先,介绍一下fastjson.fastjson是由alibaba开源的一套json处理器.与其他json处理器(如Gson,Jackson等)和其他的Java对象序列化反序列化 ...
- Django auth
auth是django一个自带的用户验证系统,使用它可以减少我们的开发流程. 基本使用 大体流程: 自定义类 from django.contrib.auth.models import Abstra ...