方法:左右两个双指针,时间复杂度O(n)
class Solution:
def maxArea(self, height: List[int]) -> int:
n = len(height)
left, right = 0, n-1
res = -1
while left < right:
tmp = min(height[left], height[right])*(right-left)
res = max(res, tmp)
if height[left] < height[right]:
left += 1
else:
right -= 1
return res
# 时间复杂度O(n^2)超时
'''
n = len(height)
res = [float('-inf')]*n
res[0] = 0
for i in range(1, n):
maxw = -1
for j in range(i):
tmp = min(height[i], height[j])*(i-j)
maxw = max(tmp, maxw)
res[i] = maxw
return max(res)
'''