题解 | #盛水最多的容器#
盛水最多的容器
https://www.nowcoder.com/practice/3d8d6a8e516e4633a2244d2934e5aa47
class Solution:
def maxArea(self, height: List[int]) -> int:
if len(height) < 2:
return 0
left = 0
right = len(height) - 1
max_area = 0
while left < right:
# 计算当前左右指针所形成的容器的面积
area = min(height[left], height[right]) * (right - left)
# 更新最大面积
max_area = max(max_area, area)
# 移动指针,由于容器的面积取决于较短的那条边,因此我们将较短的边所对应的指针向内移动
if height[left] < height[right]:
left += 1
else:
right -= 1
return max_area
查看2道真题和解析