You are given an integer array heights where heights[i] represents the height of the bar.
You may choose any two bars to form a container. Return the maximum amount of water a container can store.
Example 1:
Input: height = [1,7,2,5,4,7,3,6]
Output: 36Example 2:
Input: height = [2,2,2]
Output: 4Constraints:
2 <= height.length <= 10000 <= height[i] <= 1000
You should aim for a solution with O(n) time and O(1) space, where n is the size of the input array.
A brute force solution would be to try all pairs of bars in the array, compute the water for each pair, and return the maximum water among all pairs. This would be an O(n^2) solution. Can you think of a better way?
Can you think of an algorithm that runs in linear time and is commonly used in problems that deal with pairs of numbers? Find a formula to calculate the amount of water when we fix two heights.
We can use the two pointer algorithm. One pointer is at the start and the other at the end. At each step, we calculate the amount of water using the formula (j - i) * min(heights[i], heights[j]). Then, we move the pointer that has the smaller height value. Can you think why we only move the pointer at smaller height?
In the formula, the amount of water depends only on the minimum height. Therefore, it is appropriate to replace the smaller height value.
We try every possible pair of lines and compute the area they form.
For each pair (i, j), the height of the container is the shorter of the two lines, and the width is the distance between them.
By checking all pairs, we are guaranteed to find the maximum area.
res = 0 to track the maximum area found.i.j > i.(i, j):min(heights[i], heights[j]).j - i.res with the maximum of its current value and the new area.res.Using two pointers lets us efficiently search for the maximum area without checking every pair.
We start with the widest container (left at start, right at end).
The height is limited by the shorter line, so to potentially increase the area, we must move the pointer at the shorter line inward.
Moving the taller line never helps because it keeps the height the same but reduces the width.
By always moving the shorter side, we explore all meaningful possibilities.
l = 0r = len(heights) - 1res = 0 to store the maximum area.l < r:area = min(heights[l], heights[r]) * (r - l)res with the maximum area so far.heights[l] <= heights[r], move l right.r left.res after the pointers meet.The algorithm requires moving the pointer at the shorter height inward. Moving the taller pointer instead never increases the area (since height is limited by the shorter side) and can cause the algorithm to miss the optimal solution.
Unlike the Trapping Rain Water problem where you need to sum water trapped between bars, this problem finds a single container formed by two lines. Applying the wrong mental model leads to incorrect area calculations.
The width between indices l and r is r - l, not r - l + 1. Using the wrong formula overestimates the area by one unit for every pair, leading to incorrect results.