【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).找 ...
随机推荐
- android Fragment跳转Fragment
android Fragment跳转Fragment,最新的android studio3 在系统模板建立的BottomNavigationView 中跳转方式 此版本下不能用FragmentMana ...
- 57-Palindrome Linked List
Palindrome Linked List My Submissions QuestionEditorial Solution Total Accepted: 46990 Total Submiss ...
- SIG -MESH -1
协议栈 node:成为蓝牙mesh网络中一员的设备被称为节点(Node). 蓝牙mesh规格定义了节点可能拥有的特性.具有这些特性中的一个或多个,即表示节点可以在网络中扮演相应的特殊角色.定义的 ...
- (亿级流量)分布式防重复提交token设计
大型互联网项目中,很多流量都达到亿级.同一时间很多的人在使用,而每个用户提交表单的时候都可能会出现重复点击的情况,此时如果不做好控制,那么系统将会产生很多的数据重复的问题.怎样去设计一个高可用的防重复 ...
- Hadoop运行jar包报错java.lang.Exception: java.lang.ArrayIndexOutOfBoundsException: 1
错误信息: java.lang.Exception: java.lang.ArrayIndexOutOfBoundsException: 1 at org.apache.hadoop.mapre ...
- 学习Java 2021/10/7
java重写Override 重载Overload 重写是子类对父类的允许访问的方法的实现过程进行重新编写,返回值和形参都不能改变.即外壳不变,核心重写 重写规则: 参数列表与被重写方法的参数列表必须 ...
- Angular中@Output()的使用方法
子component中的html文件 <button (click)="Send()">送出</button><br> 子component中的 ...
- iOS-调用系统的短信和发送邮件功能,实现短信分享和邮件分享
一.邮件分享 1.iOS系统自带邮件设置邮箱(此处以QQ邮箱为例)(http://jingyan.baidu.com/album/6181c3e084cb7d152ef153b5.html?picin ...
- Linux基础命令---elinks文本浏览器
elinks elinks指令是一个纯文本格式的浏览器,支持颜色.表格.鼠标.菜单操作. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法 ...
- 通过 Ajax 发送 PUT、DELETE 请求的两种实现方式
一.普通请求方法发送 PUT 请求 1. 如果不用 ajax 发送 PUT,我们可以通过设置一个隐藏域设置 _method 的值,如下: <form action="/emps&quo ...