装水问题

给定一个数组,每个位置的数字代表高度,两个位置之间可容纳的水为位置之差与两者较低高度的乘积,输出数组中最大可容纳的水为多少

示例:

输入:[2,3,10,5,7,8,9]

输出:36

class Solution(object):
    def maxArea(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        start = 0
        end = len(height) - 1
        area = min(height[end],height[start])*(end-start)
        if end == start + 1:
            return area
        while start < end:
            while start < end and height[start] <= height[end]:
                start += 1
                if height[start] > height[start-1]:
                    area  = max(area,min(height[end],height[start])*(end-start))
            while start < end and height[end]< height[start]:
                end -= 1
                if height[end] > height[end+1]:
                    area = max(area, min(height[end], height[start]) * (end - start))
        return area

猜你喜欢

转载自www.cnblogs.com/wenqinchao/p/10557678.html