LeetCode 11. [👁] Container With Most Water & two pointers
盛最多水的容器
给定 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的更多相关文章
- 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 用双指针向中间滑动,较小的高度就作为当前情 ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode] 11. Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- [leetcode]11. Container With Most Water存水最多的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- LeetCode#11. Container With Most Water
问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- Java [leetcode 11] Container With Most Water
问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- C#解leetcode 11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
随机推荐
- SpringMVC和Struts是线程安全的吗?为什么?
线程不安全的.(其实我觉得回答为:存在线程安全问题 这样比较好点) 原因如下: 第一点,先理解为何线程不安全 1 struts1的action是单例的,所以存在线程安全问题(struts2是多例的,不 ...
- ML: 聚类算法R包 - 密度聚类
密度聚类 fpc::dbscan fpc::dbscan DBSCAN核心思想:如果一个点,在距它Eps的范围内有不少于MinPts个点,则该点就是核心点.核心和它Eps范围内的邻居形成一个簇.在一个 ...
- SDRAM---页读写
SDRAM---页读写 1.SDRAM页访问 一页通俗的来讲就是一行. SDRAM页写操作时序图: 2.DDR(经常被提起,但是我和你不熟) DDR的连续访问操作 给DDR一个write命令,同时给出 ...
- XE5 Android 开发实现手机打电话和发短信 [转]
其实都可以通过intent和URI调用系统功能.Windows程序员可以理解成是ShellExecute.这个是万金油.可以有调用各种功能.后面会介绍. 1.短信息.很简单 方法a.不使用Intent ...
- Hiero中的Events机制
The hiero.core.events module allows you to register method callbacks to respond to events sent by Hi ...
- git 克隆指定分支
git clone -b v2.8.1 https://git.oschina.net/oschina/android-app.git
- php函数总结2
1.匿名参数 func_get_args //获取参数列表的数组 func_get_arg(num) //数组索引 func_num_args() //获取匿名函数的个数 <?php funct ...
- ubuntu 17.10.1 安装 virtual box 增强工具
ubuntu 17.10.1 安装 virtual box 增强工具遇到 “ Please install the gcc make perl packages from your distribu ...
- 让SQL SERVER自动清理掉处于SLEEPING状态超过30分钟的进程(转)
原文地址:http://www.itpub.net/thread-809758-1-1.html use master go ) drop procedure [dbo].[p_killspid] G ...
- 关键两招就解决Wampserver 打开localhost显示IIS7图片问题
我们在安装集成环境Wampserver之后,有时会遇到一个问题, 打开localhost显示一张IIS7图片,这个问题该如何解决呢,我在网上找了一些说的都很乱,我在这里简单整理了一下解决方法 1 ...