@author: ZZQ

@software: PyCharm

@file: maxArea.py

@time: 2018/10/11 21:47

说明:给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

示例: 输入: [1,8,6,2,5,4,8,3,7]

输出: 49

思路:两个指针分别指向首尾,当满足 first < last时,计算面积,并判断首尾指针指向的高度哪个大,较小的一端移动指针。

class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
first = 0
last = len(height)-1
area = 0
while first < last:
area = max(area, min(height[first], height[last])*(last-first))
if height[last] > height[first]:
first += 1
else:
last -= 1
return area
if __name__ == "__main__":
answer = Solution()
print(answer.maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7]))

Leetcode题库——11.盛最多水的容器的更多相关文章

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

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

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

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

  3. leetcode刷题11. 盛最多水的容器

    做题连接https://leetcode-cn.com/problems/container-with-most-water/submissions/ 本题分为两种方法: 暴力法: int maxAr ...

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

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

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

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

  6. Leetcode 11.盛最多水的容器 By Python

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

  7. LeetCode 11 - 盛最多水的容器 - [双指针暴力]

    题目链接:https://leetcode-cn.com/problems/container-with-most-water/description/ 给定 n 个非负整数 $a_1,a_2,\cd ...

  8. leetcode题目11.盛最多水的容器(中等)

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

  9. [LeetCode]11. 盛最多水的容器(双指针)

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

随机推荐

  1. MySQL 卸载

    第一步:先查看 mysql 服务是否停止  没有停止就停止 第二步:用 管理员身份 运行 命令提示符 查看mysql 服务是否停止  : net stop mysql 卸载 : mysqld remo ...

  2. The Gene of Bitizens

    1.          Summary The document is about the general idea of the architecture design of the Bitizen ...

  3. windows下搭建permeate漏洞测试系统实战

    最近一直在搭建漏洞测试环境练习. 在此期间遇到很多问题,但是通过学习都一一解决.通过写此文来记录遇到的问题和解决方法. 首先,在github上看到了一个不错的permeate渗透测试系统.于是想搭建拿 ...

  4. x01.calc: 编程语言

    想写终极程序,大都去写操作系统或编程语言了.编程语言可以极其复杂如C,也可以极简,只处理加减乘除如 calc. 1. 词法分析 %{ #include <stdio.h> #include ...

  5. java第一天!

    public class Main { public static void main(String[] args)//main主函数 { final double PI=3.14;//定义常量,小数 ...

  6. winform 自定义控件:半透明Loading控件

    winform  自定义控件:半透明Loading控件 by wgscd date:2015-05-05 效果: using System;using System.Drawing;using Sys ...

  7. mysql是否区分大小写

    1.是否区分 库名.表名.列名.别名 的大小写? ------------------------------------------------------------------ [ Linux] ...

  8. elastic-job+zookeeper实现分布式定时任务调度的使用(springboot版本)

    总体思路,要确认一个定时任务需要一个cron表达式+jobDetail: 现在要让实现定时任务的协调,则就让zookeeper,简单说就是需要3要素,zk对象+cron+jobDetail: 总的项目 ...

  9. 11- IO模型-未完成

    1.同步.异步.阻塞.非阻塞 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么 ...

  10. idea中xml打开方式变成file,改回来

    原文:https://blog.csdn.net/u012903926/article/details/80682885 创建了一个test文件,用的是普通text打开方式,然后你修改文件为test. ...