1花墩、題目
給定一個(gè)長(zhǎng)度為 n 的整數(shù)數(shù)組 height 雳刺。有 n 條垂線盔性,第 i 條線的兩個(gè)端點(diǎn)是 (i, 0) 和 (i, height[i]) 。
找出其中的兩條線劲妙,使得它們與 x 軸共同構(gòu)成的容器可以容納最多的水湃鹊。
返回容器可以儲(chǔ)存的最大水量。
說(shuō)明:你不能傾斜容器镣奋。
2币呵、代碼
class Solution(object):
def maxArea(self, height):
"""
指針一左一右,誰(shuí)小往前走
"""
len_height = len(height)
l, r = 0, len_height - 1
max_area = 0
while l <= r:
max_area = max(max_area, min(height[l], height[r]) * (r - l))
if height[r] > height[l]:
l += 1
else:
r -= 1
return max_area
3侨颈、示例
height=[1,8,6,2,5,4,8,3,7]
s=Solution()
res=s.maxArea(height)
print(res)