LeetCode(11)Container With Most Water

时间:2022-04-15 10:14:16

题目如下:

LeetCode(11)Container With Most Water

题目的意思是求容器能装的最大的水量,当时我按梯形的面积来算,一直不对,后来才发现要按矩形的面积来算

Python代码如下:

    def maxArea(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        right = len(height)-1
        left = 0
        maxWater = 0
        while(left<right):
            maxWater = max(maxWater,min(height[left],height[right])*(right-left))
            if(height[left]<height[right]):
                left+=1
            else:
                right-=1
        return maxWater