Problem: 11. 盛最多水的容器


文章目录

  • 思路 & 解题方法
  • 复杂度
  • Code


思路 & 解题方法

之前其实做过一次这个题目,但是已经很久远了。再拿到这个题,是真的想不出来如何进行双指针。主要就是起始点需要最左和最右开始,然后不断更改矮的那个边界。

复杂度

时间复杂度:

添加时间复杂度, 示例: 盛最多水的容器【双指针】_双指针

空间复杂度:

添加空间复杂度, 示例: 盛最多水的容器【双指针】_双指针

Code

class Solution:
    def maxArea(self, height: List[int]) -> int:
        left, right = 0, len(height) - 1
        ans = 0
        while left < right:
            ans = max((right - left) * min(height[left], height[right]), ans)
            if height[left] < height[right]:
                left += 1
            else:
                right -= 1
        return ans