title: "leetcode-11 Container With Most Water 盛最多水的容器"
我的博客 https://zszdata.com/2019/03/09/count-primes/
Container With Most Water
給定 n 個非負整數 a1,a2碱鳞,...祠挫,an可训,每個數代表坐標中的一個點 (i, ai) 沈矿。在坐標內畫 n 條垂直線莉御,垂直線 i 的兩個端點分別為 (i, ai) 和 (i, 0)馍佑。找出其中的兩條線条舔,使得它們與 x 軸共同構成的容器可以容納最多的水。
說明:你不能傾斜容器怨咪,且 n 的值至少為 2屋剑。
pic
圖中垂直線代表輸入數組 [1,8,6,2,5,4,8,3,7]。在此情況下诗眨,容器能夠容納水(表示為藍色部分)的最大值為 49唉匾。
Example:
輸入: [1,8,6,2,5,4,8,3,7]
輸出: 49
Solution:
class Solution:
def maxArea(self, height: List[int]) -> int:
left = 0 #最左邊
right = len(height) - 1 #最右邊
max = 0 #初始面積為0
while left < right:
b = right - left
if height[left] < height[right]:
h = height[left]
left += 1
else:
h = height[right]
right -= 1
area = b*h
if max < area:
max = area
return max