################################################
双指针方法移动短的边使得冗余计算减少
尝试改进原方法,跳过变化后小于原长度的边,改动后效果反而变差,目测是判断语句
花费了更多的时间
################################################
class Solution:
def maxArea(self, height: List[int]) -> int:
i,j = 0,len(height)-1
Area = 0
while j>i :
if (height[i]>height[j]):
Area = max(Area,height[j]*(j-i))
j -= 1
else:
Area = max(Area,height[i]*(j-i))
i += 1
return Area
class Solution:
def maxArea(self, height: List[int]) -> int:
i,j = 0,len(height)-1
Area = 0
flag = -1
while j>i :
if (height[i]>height[j]):
if flag == 0 and height[j]<height[j+1]:
flag = 0
j -=1
else:
Area = max(Area, height[j] * (j - i))
j -= 1
flag = 0
else:
if flag ==1 and height[i-1]>height[i]:
flag = 1
i +=1
else:
Area = max(Area,height[i]*(j-i))
i += 1
flag = 1
return Area