0
点赞
收藏
分享

微信扫一扫

LeetCode刷题——11. 盛最多水的容器


题目

LeetCode刷题——11. 盛最多水的容器_对撞指针

思路

两条垂线,根据短板效应,能盛水的高度是最短的那条垂线,同时,长是两条垂线下标之差。两条垂线,出现数字2,可以考虑对撞指针。

代码

class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""

max_area = 0
i,j = 0,len(height) -1

while i < j:
# 计算面积
area = height[i] * (j-i) if height[i] < height[j] else height[j] * (j-i)
if area > max_area:
max_area = area
# 瓶颈在于短一点的垂线,因此尽量找到高一点的垂线
# height[i] 教高,则 j往左移,期望找到更高的垂线
if height[i] > height[j]:
j -= 1
else:
i += 1

return

LeetCode刷题——11. 盛最多水的容器_leetcode_02


举报

相关推荐

0 条评论