自学内容网 自学内容网

11. 盛最多水的容器

在这里插入图片描述
思路
一个容器所能盛的最大水容量取决于构成这个容器最短那条线的高度

class Solution(object):
    def maxArea(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        # 移动最短原因:移动最大值,区间内任一区间的面积都不会比大区间大  
        # 移动大值,宽度减小,此时最大值若大于L,则面积小,若小于,则长、宽都变小
        maxs = 0
        l = 0
        r = len(height)-1
        while l<r:
            areas = min(height[l],height[r])*(r-l)
            maxs = max(maxs,areas)
            if height[l]<=height[r]:
                l+=1
            else:
                r-=1
        return maxs


原文地址:https://blog.csdn.net/huanxianxianshi/article/details/143021614

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!