Container With Most Water
Choose two lines from a row of heights that hold the most water between them.
You are given an array heightiiheight[i]
Example 1
Input: height = [1, 8, 6, 2, 5, 4, 8, 3, 7]Output: 49
Explanation: the lines at positions 1 and 8 are 7 apart, and the shorter is 7 tall: 7 × 7 = 49.
Example 2
Output: 1
The Idea
Think of the lines as thin walls standing in a row. Keep any two of them and pour water in between. The water rises until it spills over the lower wall, so the lower wall sets the level. The amount of water is the distance between the two walls times that level.
Take Example 1. The two outer lines, at positions 0 and 8, are as far apart as possible: 8. But the line at position 0 is only 1 tall, so they hold 8 × 1 = 8. The lines at positions 1 and 8 are a little closer, 7 apart, and the lower of them is 7 tall. They hold 7 × 7 = 49, the answer. Look at the picture below.
The blue line at position 1, marked left in the picture, is 8 tall. The orange line at position 8, marked right, is 7 tall, so the water stops at 7, the dashed line. The width is 8 - 1 = 7. The lines in between do not get in the way: they are thin, and the water simply fills around them.
Nine lines can be paired in 36 ways. Measuring all 36 works, but the count grows with the square of the number of lines. So the real question is: can we rule out some pairs without measuring them?
Go back to the outer pair, positions 0 and 8, which hold 8. Every other partner for the line at position 0 is closer to it, so the width is smaller. The level can never be more than 1, because that line is only 1 tall. So no container with the line at position 0 can beat the 8 we just measured. One measurement is enough to rule out that line for good.
That gives the whole plan. Put one marker on each outer line and measure. Then move the marker on the shorter line one step inward and measure again. Searching with two markers that start at opposite ends and move toward each other is called the two-pointer technique.
Solution Approach
Turning that idea into code means settling three things: how to measure one container, which line to give up after each measurement, and when to stop.
Measuring one container
What does a container hold? Call the two markers leftrightright - leftMath.min(height[left], height[right])
The simple way measures every pair with two loops, one inside the other. For n lines that is n × (n - 1) / 2 pairs: 36 for nine lines, but about 5 billion for 100,000 lines.
Which line to give up
After measuring leftright
What if both lines are equally tall? Then the same reasoning holds for each of them, so neither can do better with a closer partner, and moving either one is safe. The code below moves right
When to stop
Each step moves one marker one position inward, so after at most n - 1 steps the markers meet and no container is left. In Example 1 that means 8 measurements instead of 36. Along the way, a variable best
Java Solution
The method maxArea
- First, put the two markers on the outer lines:
andleft = 0 . Setright = height.length - 1 to 0.best - Start a
loop that runs as long aswhile is left ofleft . Inside, measure the water asright , the width times the shorter line, and keep the larger ofarea andbest inarea .best - Still inside the loop, move the shorter line inward: if
is smaller,height[left] moves right, otherwiseleft moves left. Theright branch also covers equal heights.else - When the markers meet, the loop ends; return
.best
The main
public class ContainerWithMostWater {
public static int maxArea(int[] height) {
int left = 0;
int right = height.length - 1;
int best = 0;
while (left < right) {
int area = (right - left) * Math.min(height[left], height[right]);
best = Math.max(best, area);
if (height[left] < height[right]) {
left++; // the shorter line limits the area: drop it
} else {
right--;
}
}
return best;
}
public static void main(String[] args) {
System.out.println(maxArea(new int[] {1, 8, 6, 2, 5, 4, 8, 3, 7}));
System.out.println(maxArea(new int[] {1, 1}));
System.out.println(maxArea(new int[] {1, 9, 1, 1, 9, 1}));
System.out.println(maxArea(new int[] {4, 3, 2, 1, 4}));
System.out.println(maxArea(new int[] {0, 0, 0}));
}
}
Running it prints:
49
1
27
16
0
Next to 49 and 1 from the two examples, look at the third line, 27, from [1, 9, 1, 1, 9, 1]
The fourth line, 16, comes from [4, 3, 2, 1, 4]right
The direction of the move matters. A version that moves the taller line instead returns 8 for Example 1, not 49. There leftright
The original implementation,
Complexity
The work is one walk of the two markers toward each other, with one measurement per step.
- Time: O(n), where n is the number of lines. Every pass of the loop moves
orleft one position inward, so they meet after at most n - 1 passes. Measuring every pair would take O(n²).right - Space: O(1). The method keeps only the two markers, the current area and
, and never copies thebest array.height
In