python_leetcode_8_ 盛最多水的容器

################################################
双指针方法移动短的边使得冗余计算减少
尝试改进原方法,跳过变化后小于原长度的边,改动后效果反而变差,目测是判断语句
花费了更多的时间
################################################
python_leetcode_8_ 盛最多水的容器_第1张图片

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

python_leetcode_8_ 盛最多水的容器_第2张图片

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[i]:
                    flag = 1
                    i +=1
                else:
                    Area = max(Area,height[i]*(j-i))
                    i += 1
                    flag = 1
        return Area

python_leetcode_8_ 盛最多水的容器_第3张图片

你可能感兴趣的:(python_leetcode_8_ 盛最多水的容器)