盛最多水的容器

给你 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

说明:你不能倾斜容器。

class Solution(object):
    def maxArea(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        n=len(height)

        area=[]
        location=[]

        for i in range(1,n):
            for j in range(n):
                 minh=min(height[i],height[j])
                 wide=abs(height.index(height[i])-height.index(height[j]))
                 area.append(minh*wide)
                 location.append([i,j])
        max_area_index=area.index(max(area))
        print(location[max_area_index])
        return max(area)
    

c=Solution()

a=[1,8,6,2,5,4,8,3,7]
c1=c.maxArea(a)


print('c1=',c1)
    

在这里插入图片描述

输入:[1,8,6,2,5,4,8,3,7]
输出:49 
解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。

posted @ 2022-08-19 22:49  luoganttcc  阅读(6)  评论(0)    收藏  举报