盛最多水的容器

给定 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. RedHat6.5安装Spark单机

    版本号: RedHat6.5   RHEL 6.5系统安装配置图解教程(rhel-server-6.5) JDK1.8      http://blog.csdn.net/chongxin1/arti ...

  2. 网易云课堂js学习笔记

    javascript:用来在页面中编写特效的,和html/css一样都是由浏览器解析的 javascript语言: 一.js如何运行的(javaScript,jscript,vbscript,appl ...

  3. Linux常用命令之定时任务

    定时任务的实现,可以让我们把很多重复的,有规律的事情交给机器做.我们就不用苦逼的烦躁做同一件事,这样也让我们做程序的有更多的乐趣和价值.用技术的手段解决常人花时间精力解决的问题.在Linux下实现定时 ...

  4. 在Win32程序中显示Dos调试窗口

    在很多程序中,都可以看到程序运行中,会有一个Dos窗口,实时显示一些运行信息,这里就告诉大家是如何实现的,我们做个简单的,其实对控制台的操作还有很多,有兴趣的可以去查资料. 用到的API函数如下: / ...

  5. 黄聪: bootstrap 多模态框实现

    默认情况下,bootstrap模态框是不支持多个覆盖的,下面是一个解决办法(本人亲测), 将下面的代码复制到当前需要多个模态框的页面,问题就可以解决 $(document).on('show.bs.m ...

  6. BOS物流项目心得

    定区管理 (和分区有何区别) : 区域管理针对自然行政区, 行政区域比较大,不可能让取派员去负责整个行政区域, 需要进行分区,将行政区域细分 ,成为很多小区域(分区), 需要为分区知道取派人员 , 在 ...

  7. 峰Redis学习(6)Redis 数据结构(sorted-set的操作)

    第六节:Redis 数据结构之sorted-set 类型 存储Sorted-Set Sorted-Set和Set的区别   Sorted-Set中的成员在集合中的位置是有序的   存储Sorted-s ...

  8. 关于requests的session方法保持不了cookie的问题。(seesion的意思是保持一个会话,比如 登陆后继续操作(记录身份信息) 而requests是单次请求的请求,身份信息不会被记录)

    最近在做爬虫的时候遇到了一个问题,在用requests的session方法保持cookie的时候发现requests不能保持手动构建的cookie.起初以为requests只能自动保持由服务器返回的s ...

  9. Java工程师 必须掌握的知识点

    Web核心:XML.HTTP及Tomcat.Servlet.request与response.cookie与session.jsp技术.jdbc高级.Ajax开发.Filter/Listener高级. ...

  10. MySQL数据库事务各隔离级别加锁情况--read committed && MVCC(转载)

    http://www.imooc.com/article/17290 http://www.51testing.com/html/38/n-3720638.html https://dev.mysql ...