Container With Most Water in DSA Javascript - Time & Space Complexity
We want to know how the time needed to find the container with the most water changes as the number of lines grows.
How does the number of operations grow when we try all pairs or use a smarter way?
Analyze the time complexity of the following code snippet.
function maxArea(height) {
let left = 0, right = height.length - 1;
let max = 0;
while (left < right) {
const width = right - left;
const area = Math.min(height[left], height[right]) * width;
max = Math.max(max, area);
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return max;
}
This code finds the largest area between two lines by moving pointers inward.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves two pointers inward.
- How many times: At most once per element from both ends, so about n times total.
As the number of lines (n) grows, the loop runs roughly n times, checking pairs from outside to inside.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The number of operations grows linearly with input size.
Time Complexity: O(n)
This means the time to find the max area grows in a straight line as the number of lines increases.
[X] Wrong: "We must check every pair of lines, so it takes n squared time."
[OK] Correct: The two-pointer method smartly skips unnecessary pairs, so it only needs to check about n pairs, not all n².
Understanding this approach shows you can find efficient solutions by reducing repeated work, a key skill in problem solving.
"What if we used a nested loop to check all pairs instead of two pointers? How would the time complexity change?"
Practice
Solution
Step 1: Understand the problem statement
The problem asks to find two lines that together with the x-axis form a container that holds the most water.Step 2: Identify what to maximize
The area between two lines is width (distance between lines) times the height (minimum of the two lines). We want the maximum such area.Final Answer:
The maximum area formed between two lines representing container walls. -> Option AQuick Check:
Max area = max water container [OK]
- Thinking it's about sum of heights
- Confusing max height with max area
- Ignoring the distance between lines
Solution
Step 1: Identify correct pointer initialization
We start with left pointer at index 0 and right pointer at last index (length - 1).Step 2: Check each option
let left = 0, right = heights.length - 1; correctly sets left = 0 and right = heights.length - 1. Others use invalid indices.Final Answer:
let left = 0, right = heights.length - 1; -> Option DQuick Check:
Start pointers at array ends [OK]
- Using invalid indices outside array bounds
- Starting both pointers at same position
- Confusing length with last index
function maxArea(height) {
let left = 0, right = height.length - 1;
let max = 0;
while (left < right) {
const area = Math.min(height[left], height[right]) * (right - left);
max = Math.max(max, area);
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return max;
}
console.log(maxArea([1,8,6,2,5,4,8,3,7]));Solution
Step 1: Trace the two-pointer approach
Start with left=0 (height=1), right=8 (height=7), area = min(1,7)*8=8. Max=8.Step 2: Move pointers and update max
Since height[left]Final Answer:
49 -> Option BQuick Check:
Max area for input = 49 [OK]
- Calculating area incorrectly using max height
- Not updating max area properly
- Moving wrong pointer in the loop
function maxArea(height) {
let left = 0, right = height.length - 1;
let max = 0;
while (left < right) {
const area = Math.min(height[left], height[right]) * (right - left);
max = Math.max(max, area);
if (height[left] < height[right]) {
left++;
} else {
right++;
}
}
return max;
}Solution
Step 1: Check pointer movement logic
The right pointer is incorrectly incremented (right++), which moves it out of array bounds.Step 2: Correct pointer movement
When height[left] >= height[right], right pointer should move left (right--) to find a potentially larger area.Final Answer:
The right pointer should decrement (right--) instead of increment (right++). -> Option AQuick Check:
Right pointer moves left to reduce width [OK]
- Using right++ instead of right--
- Loop condition allowing left == right
- Incorrect area calculation using max height
Solution
Step 1: Add variables to track indices
Initialize variables to store left and right indices when max area updates.Step 2: Update indices when max area changes
Inside the loop, when a new max area is found, save current left and right indices.Step 3: Return max area and indices
Return an object or array containing max area and the two indices after loop ends.Final Answer:
Track indices when updating max area and return them along with max area. -> Option CQuick Check:
Save indices on max update for output [OK]
- Returning fixed indices without checking area
- Sorting array loses original indices
- Using nested loops increases time complexity
Ready to prove it?
Pass the test and this pattern moves unknown
