class Solution:
def maxArea(self, height: List[int]) -> int:
i = 0
j = len(height) - 1
Lmax = 0
Rmax = 0
areaMax = 0
while i<j:
# if height[i]> Lmax : Lmax = height[i]
# if height[j]> Rmax : Rmax = height[j]
area = (j-i)*min(height[i],height[j])
if area > areaMax : areaMax = area
if height[i]< height[j] :
i+=1
else :
j-=1
return areaMax
图片.png
别人写的结构就很优秀了
class Solution:
def maxArea(self, height: List[int]) -> int:
i, j, res= 0, len(height) - 1, 0
while i < j:
if height[i] < height[j]:
res = max(res, height[i] * (j - i))
i += 1
else:
res = max(res, height[j] * (j - i))
j -= 1
return res