11. Container With Most Water

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container and n is at least 2.

能装最多水的容器是多大?  这里s=(j-i)*min(a[i],a[j])

方法一,暴力搜索 for i range(n): for j in range(i+1,n):

方法二,从两边往中间夹,假设最开始(0,3)(10,4)两个点(start,end),要比这个容器大,一定不包括(0,3)这个点。

class Solution:
    def maxArea(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        l=0
        r=len(height)-1
        maxarea=0
        while l<r:
            Area=min(height[l],height[r])*(r-l)
            if maxarea<Area:
                maxarea=Area
            if height[l]>height[r]:   
                r-=1
            else:
                l+=1
        return maxarea

posted @ 2018-06-14 15:18  zx-y  阅读(114)  评论(0编辑  收藏  举报