11.盛最多水的容器

給定 n 個非負整數 a1,a2,...,an,每個數代表座標中的一個點 (i, ai) 。在座標內畫 n 條垂直線,垂直線 i 的兩個端點分別爲 (i, ai) 和 (i, 0)。找出其中的兩條線,使得它們與 x 軸共同構成的容器可以容納最多的水。

說明:你不能傾斜容器,且 n 的值至少爲 2。

class Solution:
    def maxArea(self, height: List[int]) -> int:
        n,m = 0, len(height)-1
        result = 0
        while n < m:
            if height[n] < height[m]:
                result = max(result, height[n]*(m-n))
                n += 1
            else:
                result = max(result, height[m]*(m-n))
                m -= 1
        return result
        

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章