盛最多水的容器

给定 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. RSA读取密钥——使用openssl编程

    RSA是基于数论中大素数的乘积难分解理论上的非对称加密法.在此密码术中,使用公钥(public key)和私钥(private key)两个不同的密钥:公钥用于加密,它是向所有人公开的:私钥用于解密, ...

  2. Qt中的主窗口之菜单栏

    1.Qt中的主窗口 主窗口为建立应用程序用户界面提供了一个框架 Qt开发平台中直接支持主窗口的概念 QMainWindow是Qt中主窗口的基类 QMainWindow继承于QWidget是一种容器类型 ...

  3. 基于Eclipse搭建hadoop开发环境

    一.基础环境准备 1.Eclipse 下载地址:http://pan.baidu.com/s/1slArxAP 2.JDK1.8  下载地址:http://pan.baidu.com/s/1i5iNy ...

  4. git 克隆指定分支

    git clone -b v2.8.1 https://git.oschina.net/oschina/android-app.git

  5. WPF DataGrid添加编号列

    WPF DataGrid添加编号列? 第一步:<DataGridTemplateColumn Header="编号" Width="50" MinWidt ...

  6. workerman 简单笔记

    运行命令 curl -Ss http://www.workerman.net/check.php | php     都显示的ok (注意:检测脚本中没有检测event扩展或者libevent扩展) ...

  7. json server的简单使用(附:使用nodejs快速搭建本地服务器)

    作为前端开发人员,经常需要模拟后台数据,我们称之为mock.通常的方式为自己搭建一个服务器,返回我们想要的数据.json server 作为工具,因为它足够简单,写少量数据,即可使用. 安装 首先需要 ...

  8. 解决Delphi 2010启动时卡死并报“displayNotification: 堆栈溢出”错误

    1. 清理IE的历史记录,删除浏览器缓存(不需要清cookie) 2. 禁用startpage 2.1 从 Delphi 2010 启动菜单上点右键 -> 查看属性->快捷方式->目 ...

  9. 玄学曲线并不玄 教你如何看懂GPU呈现

    红色代表了“执行时间”,它指的是Android渲染引擎执行盒子中这些绘制命令的时间,假如当前界面的视图越多,那么红色便会“跳”得越高.实际使用中,比如我们平时刷淘宝App时遇到出现多张缩略图需要加载时 ...

  10. 【Mysql】mysql使用触发器创建hash索引

    概述 若设计的数据表中,包含较长的字段,比如URL(通常都会比较长),查询时需要根据该字段进行过滤: select * from table_xxx where url = 'xxxxxxx'; 为了 ...