# \[11]\[中等]\[双指针] 盛最多水的容器

## 题目描述

[11. 盛最多水的容器](https://leetcode-cn.com/problems/container-with-most-water/)

给你 n 个非负整数 a1，a2，...，an，每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线，垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) 。找出其中的两条线，使得它们与 x 轴共同构成的容器可以容纳最多的水。

说明：你不能倾斜容器。

示例 1：

![](/files/-MWvK3YHFb7SG_G7Vw7x)

```
输入：[1,8,6,2,5,4,8,3,7]
输出：49 
解释：图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下，容器能够容纳水（表示为蓝色部分）的最大值为 49。
```

示例 2：

```
输入：height = [1,1]
输出：1
```

示例 3：

```
输入：height = [4,3,2,1,4]
输出：16
```

示例 4：

```
输入：height = [1,2,1]
输出：2
```

提示：

* n = height.length
* 2 <= n <= 3 \* 104
* 0 <= height\[i] <= 3 \* 104

## 解题思路

使用左右两个指针, 开始的时候分别指向数组的左右两端, 可以计算此时的容量. 计算容量时, 公式为:

* **两个指针指向的数字中较小值 ∗ 指针之间的距离**

我们逐渐将左右两个指针向内移动, 有一点可以确定的是, 移动后容器的宽度减小, 因此只有更高的容器高度, 才可能使得移动后的容器容积更大.

那么移动左右那个指针呢? 由于容器的高度, 是两个指针指向的数字中较小值, 因此如果移动较大值的指针, 这个高度是不变的, 而宽度减小的同时, 容器的容积只会缩小.

从这个角度考虑需要移动左右两个指针中, 指向数字较小的那个指针.

进一步证明合理性. 如果移动了某个指针, 那么这个位置之后就再也不可能参与到计算当中了. 按照上面的移动方式, 较小的边界会被移动. 我们可以假设这个边界如果之后还要参与到计算中, 说明另一侧指针移动了, 但容器的高度最大值已经这个较小值限定了, 因此容器只会更小.

更高的一侧后续还有机会, 与其他的位置形成高度更高的容器.

```python
class Solution:
    def maxArea(self, height: List[int]) -> int:
        n = len(height)
        if n < 2:
            return 0

        left, right = 0, n - 1
        max_area = 0
        while left < right:
            max_area = max(max_area, min(height[left], height[right]) * (right - left))
            raw_left, raw_right = left, right
            if height[left] <= height[right]:
                while left < right and height[left] <= height[raw_left]:
                    left += 1
            else:
                while left < right and height[right] <= height[raw_right]:
                    right -= 1
        return max_area
```

## 参考资料

* [盛最多水的容器](https://leetcode-cn.com/problems/container-with-most-water/solution/sheng-zui-duo-shui-de-rong-qi-by-leetcode-solution/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kerasnoone.gitbook.io/garnet/suan-fa/shuang-zhi-zhen/11-sheng-zui-duo-shui-de-rong-qi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
