You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 lines
835 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

class Solution:
def maxArea(self, height) -> int:
# 设x,y是某两根柱子的索引
# 要找的是表达式 abs(x-y) * min(height[x],height[y]) 之最大值
# 初始化两个指针,分别指向数组头和尾的索引
# 若向内移动长板,min()函数的值不变或变小s必然变小
# 若向内移动短板,min()函数的值可能变大s可能变大
i,j = 0,len(height) - 1
cur_max = (j-i) * min(height[i],height[j])
while i<j:
dis = j - i
cur_v = dis * min(height[i],height[j])
if cur_v > cur_max:
cur_max = cur_v
else:
pass
if height[i] <= height[j]:
i += 1
else:
j -= 1
return cur_max