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). ...
随机推荐
- 学习大数据基础框架hadoop需要什么基础
什么是大数据?进入本世纪以来,尤其是2010年之后,随着互联网特别是移动互联网的发展,数据的增长呈爆炸趋势,已经很难估计全世界的电子设备中存储的数据到底有多少,描述数据系统的数据量的计量单位从MB(1 ...
- scala IDE for Eclipse开发Spark程序
1.开发环境准备 scala IDE for Eclipse:版本(4.6.1) 官网下载:http://scala-ide.org/download/sdk.html 百度云盘下载:链接:http: ...
- 【java】break,continue和return区别
break:适用于switch和loop continue:只适用于loop 两者都可以通过给循环加标签来控制跳出,如下例所示 class BreakDemo { public static void ...
- .net webapi 收不到json 实体类参数,返回的json中带有k__BackingField
案例:实体类是从WCF项目中复制到webapi项目中,去掉了[DataContract],[DataMember],但[Serializable] 没去掉. 在ApiController 中,实体类输 ...
- STM32 printf函数
/******************** (C) COPYRIGHT 2012 WildFire Team *************************** * 文件名 :usart1.c * ...
- WPF Demo15 MVVM
项目结构如下: <Window x:Class="MVVMDemo.MainWindow" xmlns="http://schemas.microsoft.com/ ...
- 为IE内核的WebBrowser控件内存泄漏所烦恼的可以考虑用Cefsharp代替它!
为IE内核的WebBrowser控件内存泄漏所烦恼的朋友们,可以考虑用Cefsharp代替WebBrowser控件 特意做了一个程序来测试 利用Cefsharp做控件,访问网站.每分钟刷新2次,初始时 ...
- github简单命令
1.安装yum install -y git 2.配置帐户(github.com注册)git config --global user.name goozgkgit config --global u ...
- 基于单片机的Wifi温度湿度测量仪
这次的制作背景是由于单片机课程实训课程要求 刚好手上有块ESP8266-12F的WiFi模块 于是就选择了制作一个基于单片机,使用WiFi传输数据的温湿度采集测量仪 制作过程: 由于有使用过WiFi模 ...
- What does "exceeded limit of maxWarmingSearchers=X" mean?
Whenever a commit happens in Solr, a new "searcher" (with new caches) is opened, "war ...