# 11. Container with Most Water

## Problem

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container and n is at least 2.

**Related Topics:**

`Array` `Two Pointers`

## Analysis

![](/files/-LD0bMkBM3OFB3PvyxLR)

一个容器所能装的水量为： `length * min(height[l] , height[r])`。

`l` 与 `r` 分别从两端开始，向中间遍历。当 `height[l] < height[r]` 时，最小边为 `l`，移动 `r` 并不能扩大面积，所以移动 `l`。反之，移动 `r` 即可。

## Code

```kotlin
class Solution {

    fun maxArea(height: IntArray): Int {

        var max = 0
        var l = 0
        var r = height.size - 1

        while (l < r) {
            max = maxOf(max, (r - l) * if (height[l] < height[r]) height[l++] else height[r--])
        }

        return max
    }
}
```


---

# 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://pachirisu.gitbook.io/leetcode-algorithms/11.-container-with-most-water.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.
