Maximum Water Trapped Between Two Vertical Lines
Solve this Problemheight where height[i] is the height of a vertical line standing at index i, pick two lines that, together with the x-axis, form a container. Return the maximum amount of water that container can hold — min(height[i], height[j]) × (j - i) for the pair you pick.
Checking every pair works but wastes time re-deriving what a single pass can rule out: since water level is always capped by the shorter of the two chosen lines, a two-pointer sweep that always advances the shorter side finds the answer in O(n).
Test Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
2 ≤ height.length ≤ 10⁵ - ◆
0 ≤ height[i] ≤ 10⁴
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
| 1 | class Solution { |
| 2 | public int maxWaterBetweenLines(int[] height) { |
| 3 | int left = 0, right = height.length - 1; |
| 4 | int maxArea = 0; |
| 5 | while (left < right) { |
| 6 | int width = right - left; |
| 7 | int shorter = Math.min(height[left], height[right]); |
| 8 | int area = shorter * width; |
| 9 | maxArea = Math.max(maxArea, area); |
| 10 | if (height[left] < height[right]) { |
| 11 | left++; |
| 12 | } else { |
| 13 | right--; |
| 14 | } |
| 15 | } |
| 16 | return maxArea; |
| 17 | } |
| 18 | } |
| 19 |
050Set left and right at the two ends of the array, and maxArea to 0 — this is the widest container we could try.
Approach & Solutions
Brute Force
BruteTry every pair of lines (i, j) with two nested loops. For each pair, compute min(height[i], height[j]) * (j - i) and keep the largest value seen. Correct, but it revisits pairs that a smarter sweep can rule out without ever computing them.
O(n²)O(1)1class Solution {
2 public int maxWaterBetweenLines(int[] height) {
3 int maxArea = 0;
4 for (int i = 0; i < height.length; i++) {
5 for (int j = i + 1; j < height.length; j++) {
6 int area = Math.min(height[i], height[j]) * (j - i);
7 maxArea = Math.max(maxArea, area);
8 }
9 }
10 return maxArea;
11 }
12}Optimal — Two Pointers
OptimalStart with left at index 0 and right at the last index — the widest possible container. At each step, the water level is capped by the shorter of the two lines, so keeping the shorter one and shrinking the width can never help. Move the pointer at the shorter line inward, hoping to find something taller, and track the best area seen along the way.
O(n)O(1)1class Solution {
2 public int maxWaterBetweenLines(int[] height) {
3 int left = 0, right = height.length - 1;
4 int maxArea = 0;
5 while (left < right) {
6 int width = right - left;
7 int shorter = Math.min(height[left], height[right]);
8 int area = shorter * width;
9 maxArea = Math.max(maxArea, area);
10 if (height[left] < height[right]) {
11 left++;
12 } else {
13 right--;
14 }
15 }
16 return maxArea;
17 }
18}