美文网首页
2020-02-29 Day7: Leetcode: 11. C

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

作者: YueTan | 来源:发表于2020-02-29 15:25 被阅读0次

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

相关文章

网友评论

      本文标题:2020-02-29 Day7: Leetcode: 11. C

      本文链接:https://www.haomeiwen.com/subject/ymwphhtx.html