title: "leetcode-11 Container With Most Water 盛最多水的容器"
我的博客 https://zszdata.com/2019/03/09/count-primes/
Container With Most Water
給定 n 個非負(fù)整數(shù) a1,a2,...,an,每個數(shù)代表坐標(biāo)中的一個點(diǎn) (i, ai) 。在坐標(biāo)內(nèi)畫 n 條垂直線,垂直線 i 的兩個端點(diǎn)分別為 (i, ai) 和 (i, 0)。找出其中的兩條線,使得它們與 x 軸共同構(gòu)成的容器可以容納最多的水。
說明:你不能傾斜容器,且 n 的值至少為 2。

pic
圖中垂直線代表輸入數(shù)組 [1,8,6,2,5,4,8,3,7]。在此情況下,容器能夠容納水(表示為藍(lán)色部分)的最大值為 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