# Container With Most Water

## Solution 1 (Java)

```java
class Solution {
    public int maxArea(int[] height) {
        int maxArea = 0;
        for (int i = 0; i < height.length; i++) {
           for (int j = i + 1; j < height.length; j++) {
               int w = j - i;
               int h = Math.min(height[i], height[j]);
               maxArea = Math.max(maxArea, w * h);
           }
        }
        return maxArea;
    }
}
```

This is the brute force solution that iterates every possible areas that can be formed by the heights. This will not pass the LeetCode time constraint despite of its correctness.

## Solution 2 (Java)

```java
class Solution {
    public int maxArea(int[] height) {
        int left = 0, right = height.length - 1;
        int maxArea = 0;
        while (left < right) {
            int w = right - left;
            int h = Math.min(height[left], height[right]);
            maxArea = Math.max(maxArea, w * h);
            if (height[left] < height[right]) {
                left++;
            } else {
                right--;
            }
        }
        return maxArea;
    }
}
```

This is the two-pointer approach.&#x20;

The best intuition for this approach I have come across is the following: To ensure the maximum area, there are two things in consideration: height and width. To start with, we have already had the largest width (by starting `left = 0` and `right = height.length - 1`. When we consider `left` and `right`, if the `left` is shorter, we move the `left` pointer to the right because the current area is already the maximum area that can be formed by the `left` height. Namely, any movement of the `right` pointer to left would decrease the area. The same is for the other case for the `right` pointer.

You may also do a rigorous proof such as proof by contradiction.

TL;DR: Each step we are can only be improving the situation (not making it worse, i.e., decreasing the area). This is similar to the idea of proving a greedy algorithm.


---

# 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://blog.yushunchen.com/algo/array/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.
