2020-02-29 Day7: Leetcode: 11. C

2020-02-29  本文已影响0人  YueTan

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.

class Solution:
    def maxArea(self, height: List[int]) -> int:        
        start,end=0,len(height)-1
        max_height=0
        
        while start<end:
            this_height=(end-start)*min(height[start],height[end])
            max_height=max(this_height,max_height)
            
            if height[start]<height[end]:
                start+=1
            else:
                end-=1
        return max_height
上一篇 下一篇

猜你喜欢

热点阅读