题解 | #盛水最多的容器#
盛水最多的容器
http://www.nowcoder.com/practice/3d8d6a8e516e4633a2244d2934e5aa47
优化过的双指针
class Solution:
def maxArea(self, height: List[int]) -> int:
if not height:
return 0
res = 0
left = 0
right = len(height) - 1
max_height = max(height)
while left < right:
tmp_left = height[left]
tmp_right = height[right]
if tmp_left < tmp_right:
res = max(res, tmp_left * (right - left))
if tmp_left == max_height:
break
while left < right and height[left] <= tmp_left:
left += 1
else:
res = max(res, tmp_right * (right - left))
if tmp_right == max_height:
break
while left < right and height[right] <= tmp_right:
right -= 1
return res