• 作者: 负雪明烛
  • 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:

  1. Input: [1,8,6,2,5,4,8,3,7]
  2. Output: 49

题目大意

有很多挡板,从这些挡板中选两个,然后计算能够成的面积的最大值。

解题方法

双指针

如果纯暴力计算两两之间的面积,时间复杂度是O(N^2),肯定会超时。

一个比较好的解决的方法是,使用双指针方法,一个从最左边开始,一个从最右边开始,计算两个挡板之间的面积,然后在向中间移动。移动的规则是这样的,如果哪个挡板比较矮,就舍弃掉这个挡板,把指向这个挡板的指针向中间移动。

这样的移动方式是我们每次都保留了比较长的哪个挡板,也就能获得更多的水。当两个挡板的高度一样的话,移动任意一个即可,因为这两个是高度一样的挡板,如果中间有更高的挡板,那么当前的挡板决定了以后的挡板的最低值,也就是说以其中任意一个为边界的容器面积不可能超过当前的当前的值。

我们在遍历的过程中需要保留遍历时候得到的最大值,最后返回即可。

时间复杂度是O(N),空间复杂度是O(1).

代码如下:

  1. class Solution(object):
  2. def maxArea(self, height):
  3. """
  4. :type height: List[int]
  5. :rtype: int
  6. """
  7. ans = 0
  8. l = 0
  9. r = len(height) - 1
  10. while l < r:
  11. ans = max(ans, min(height[l], height[r]) * (r - l))
  12. if height[l] < height[r]:
  13. l += 1
  14. else:
  15. r -= 1
  16. 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 盛最多水的容器的更多相关文章

  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. Container With Most Water 装最多水的容器

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

  3. 011 Container With Most Water 盛最多水的容器

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

  4. Leetcode11.Container With Most Water盛最多水的容器

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

  5. [LeetCode] Container With Most Water 装最多水的容器

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

  6. [LintCode] Container With Most Water 装最多水的容器

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

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

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

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

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

  9. LeetCode 11. [👁] Container With Most Water & two pointers

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

随机推荐

  1. android Fragment跳转Fragment

    android Fragment跳转Fragment,最新的android studio3 在系统模板建立的BottomNavigationView 中跳转方式 此版本下不能用FragmentMana ...

  2. 57-Palindrome Linked List

    Palindrome Linked List My Submissions QuestionEditorial Solution Total Accepted: 46990 Total Submiss ...

  3. SIG -MESH -1

    协议栈    node:成为蓝牙mesh网络中一员的设备被称为节点(Node). 蓝牙mesh规格定义了节点可能拥有的特性.具有这些特性中的一个或多个,即表示节点可以在网络中扮演相应的特殊角色.定义的 ...

  4. (亿级流量)分布式防重复提交token设计

    大型互联网项目中,很多流量都达到亿级.同一时间很多的人在使用,而每个用户提交表单的时候都可能会出现重复点击的情况,此时如果不做好控制,那么系统将会产生很多的数据重复的问题.怎样去设计一个高可用的防重复 ...

  5. Hadoop运行jar包报错java.lang.Exception: java.lang.ArrayIndexOutOfBoundsException: 1

    错误信息: java.lang.Exception: java.lang.ArrayIndexOutOfBoundsException: 1    at org.apache.hadoop.mapre ...

  6. 学习Java 2021/10/7

    java重写Override 重载Overload 重写是子类对父类的允许访问的方法的实现过程进行重新编写,返回值和形参都不能改变.即外壳不变,核心重写 重写规则: 参数列表与被重写方法的参数列表必须 ...

  7. Angular中@Output()的使用方法

    子component中的html文件 <button (click)="Send()">送出</button><br> 子component中的 ...

  8. iOS-调用系统的短信和发送邮件功能,实现短信分享和邮件分享

    一.邮件分享 1.iOS系统自带邮件设置邮箱(此处以QQ邮箱为例)(http://jingyan.baidu.com/album/6181c3e084cb7d152ef153b5.html?picin ...

  9. Linux基础命令---elinks文本浏览器

    elinks elinks指令是一个纯文本格式的浏览器,支持颜色.表格.鼠标.菜单操作. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora.   1.语法       ...

  10. 通过 Ajax 发送 PUT、DELETE 请求的两种实现方式

    一.普通请求方法发送 PUT 请求 1. 如果不用 ajax 发送 PUT,我们可以通过设置一个隐藏域设置 _method 的值,如下: <form action="/emps&quo ...