盛最多水的容器

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

看起来遍历求面积就可以了

first submission
class Solution:
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
# max area
maxA=0 for k,h in enumerate(height):
for k2,h2 in enumerate(height):
if k==k2:
continue width=abs(k2-k)
lowHeight=h if h2>=h else h2
area=width*lowHeight if maxA<area:
maxA=area return maxA

Time Limit Exceeded:

Last executed input:
[28,342,418,...,869,766,452]

spend time : 6-7.2642898

用了六七秒这么久,一定是我太蠢了。试了一阵还是得俩for遍历才能算出所有面积比对。那就想办法优化一下O(n^2)。看起来好像没法优化了。

要不从中间截一下,取平均值,对。

还是超时的,再卡卡这个范围。找找其他解法,

目标,缩短到500ms

发现related topics有双指针标签Two Pointers,去搜索了解下,太机智了。看了解答也是用双指针。

解法就是固定宽度,然后去找较高的,这样O(n)就解决了。

second submission
 class Solution:
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
# max area
maxA=0
l=len(height) i=0
j=len(height)-1 while i<=j and j<l:
lowHeight=height[i] if height[i]<=height[j] else height[j] area=lowHeight*(j-i)
if area>maxA:
maxA=area if height[i]>height[j]:
j-=1
else:
i+=1
#print(i,j) return maxAa

spend time: 0.00233s

AC! (以后看了解答的,就在名字前放个眼睛。)

总结一下双指针:

两个游标,头尾各一个,偏向满足的条件则不动,另一个朝对方移动,直到相遇

双指针活活的把O(n^2)问题变成了O(n),从6~7s缩短到2ms要是我肯定想一个月都想不到,到自己的思维怪圈就出不来了。不能靠蛮力取胜,要加强自己的基础。

扩展阅读

LeetCode 11. [👁] Container With Most Water & two pointers的更多相关文章

  1. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  2. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  3. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  4. LeetCode 11. Container With Most Water (装最多水的容器)

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

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

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

  6. [leetcode]11. Container With Most Water存水最多的容器

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

  7. LeetCode#11. Container With Most Water

    问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...

  8. Java [leetcode 11] Container With Most Water

    问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...

  9. C#解leetcode 11. Container With Most Water

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

随机推荐

  1. kettle判断是否是数据首行(第一行)

    可以用组件:分析查询(Analytic Query) 后面可以通过判断pre_id 是否为空值(null)判断.

  2. nginx基于目录的映射:

    nginx基于目录的映射: location /wxchat/ { #proxy_redirect off; proxy_set_header Host $host; proxy_set_header ...

  3. [转]windows环境下使用virtualenv对python进行多版本隔离

    windows环境下使用virtualenv对python进行多版本隔离 最近在用python做一个文本的情感分析的项目,用到tensorflow,需要用python3的版本,之前因为<机器学习 ...

  4. 写了一个hiero检查任务渲染结果的脚本

    基本思路是写了一个时间判断函数(postSequence_check)来对比transcode任务提交时间和目标文件夹内文件的修改时间来确定渲染是否成功执行,然后通过Hiero提供的postSeque ...

  5. QTreeWidget的Item点击事件

    转载:cw123458945 #!/usr/bin/env python import sys from PyQt4.QtCore import SIGNAL from PyQt4.QtGui imp ...

  6. http系列(一)

    一.关于Url URI由URL和URN组成,URI即统一资源标识符,URL即统一资源定位符,URN即统一资源名称. 现在最常用的是URL. 二.http请求/响应报文 请求报文:请求行.请求头部.空行 ...

  7. PAT 乙级 1020 月饼 (25) C++版

    1020. 月饼 (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 月饼是中国人在中秋佳节时吃的一种传统食 ...

  8. client家族属性

    在前面总结了offset家族属性和scroll家族属性,今天来总结一下client家族属性,同前面一样,client家族也包宽高和左上,具体的通过代码来区别这三大家族属性的不同. <!DOCTY ...

  9. 字符串分割split()

    知识讲解: split() 方法将字符串分割为字符串数组,并返回此数组. 语法: stringObject.split(separator,limit) 参数说明: 注意:如果把空字符串 (" ...

  10. PyCharm的一些设置

    设置使用UTF-8 在任何情况下: 设置写python脚本,新建 脚本的时候默认加的头文件. # !/usr/bin/env python# -*- coding:utf-8 -*-# Author: ...