Discover how a simple two-pointer trick saves you from endless, tiring checks!
Why Container With Most Water in DSA Typescript?
Imagine you have a row of vertical lines on a graph, each with different heights. You want to find two lines that, together with the x-axis, can hold the most water. Doing this by checking every possible pair manually is like trying to find the biggest container by measuring every pair of walls one by one.
Checking every pair of lines manually means a lot of repeated work. For example, if you have 100 lines, you would need to check 4,950 pairs! This is slow and tiring, and easy to make mistakes. It's like trying to find the tallest person in a crowd by asking every pair if they are taller than the other.
The "Container With Most Water" approach uses two pointers starting at both ends of the line array. It moves the pointer with the smaller height inward, skipping unnecessary checks. This way, it quickly finds the best container without checking every pair, saving time and effort.
let maxArea = 0; for (let i = 0; i < heights.length; i++) { for (let j = i + 1; j < heights.length; j++) { let area = Math.min(heights[i], heights[j]) * (j - i); if (area > maxArea) maxArea = area; } }
let left = 0, right = heights.length - 1, maxArea = 0; while (left < right) { let area = Math.min(heights[left], heights[right]) * (right - left); maxArea = Math.max(maxArea, area); if (heights[left] < heights[right]) left++; else right--; }
This method lets you find the largest container quickly, even with thousands of lines, making your program fast and efficient.
Think of trying to find the widest and tallest gap between buildings to hold the most water during a rainstorm. Instead of measuring every gap, you start from the edges and move inward, quickly spotting the best spot.
Manual checking every pair is slow and inefficient.
Two-pointer technique smartly skips unnecessary checks.
Efficiently finds the maximum water container in linear time.
Practice
Solution
Step 1: Understand the problem goal
The problem asks to find two lines that together with the x-axis form a container holding the most water, which means the largest area between two heights.Step 2: Identify what area means here
The area is calculated by the distance between two lines multiplied by the smaller height of the two lines.Final Answer:
The maximum area formed between two lines representing heights -> Option AQuick Check:
Container area = max area between two heights [OK]
- Confusing area with sum of heights
- Choosing tallest lines without considering distance
- Ignoring the smaller height between two lines
Solution
Step 1: Identify input and output types
The input is an array of numbers representing heights, so type is number[]. The output is a single number representing max area.Step 2: Match function signature
function maxArea(height: number[]): number matches input as number[] and output as number, which is correct for this problem.Final Answer:
function maxArea(height: number[]): number -> Option AQuick Check:
Input array of numbers, output number [OK]
- Using wrong input type like string or boolean
- Returning array instead of single number
- Mismatching input and output types
function maxArea(height: number[]): number {
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: Understand the two-pointer approach
Start with left=0 and right=8 (length-1). Calculate area = min(height[left], height[right]) * (right-left). Move pointer with smaller height inward.Step 2: Trace key iterations
Initial area: min(1,7)*8=8
Move left to 1 (height=8), area: min(8,7)*7=49 (max updated)
Move right to 7 (height=3), area: min(8,3)*6=18
Move right to 6 (height=8), area: min(8,8)*5=40
Move right to 5 (height=4), area: min(8,4)*4=16
Move right to 4 (height=5), area: min(8,5)*3=15
Move right to 3 (height=2), area: min(8,2)*2=4
Move right to 2 (height=6), area: min(8,6)*1=6
Max area found is 49.
Continue moving left pointer:
left=2 (height=6), right=2 (height=6) loop ends.
Actually, the maximum area is found earlier at 49, but let's check if any larger area exists.
Check area at left=1, right=8: min(8,7)*7=49
Check area at left=1, right=6: min(8,8)*5=40
Check area at left=2, right=8: min(6,7)*6=42
Check area at left=1, right=7: min(8,3)*6=18
Check area at left=0, right=8: min(1,7)*8=8
Check area at left=1, right=5: min(8,4)*4=16
Check area at left=1, right=4: min(8,5)*3=15
Check area at left=1, right=3: min(8,2)*2=4
Check area at left=1, right=2: min(8,6)*1=6
Check area at left=2, right=7: min(6,3)*5=15
Check area at left=2, right=6: min(6,8)*4=24
Check area at left=3, right=6: min(2,8)*3=6
Check area at left=4, right=6: min(5,8)*2=10
Check area at left=5, right=6: min(4,8)*1=4
Maximum area is 49.
However, the known correct answer for this input is 49, but the official LeetCode solution and common knowledge say the max area is 49.
Therefore, correct answer is 49.Final Answer:
49 -> Option BQuick Check:
Max area = 49 [OK]
- Calculating area with max height instead of min
- Not updating max area correctly
- Moving wrong pointer in the loop
function maxArea(height: number[]): number {
let left = 0;
let 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 algorithm moves the pointer with smaller height inward to try larger area. The code moves left pointer if height[left] > height[right], which is incorrect.Step 2: Correct pointer movement condition
It should move left pointer if height[left] < height[right], else move right pointer. This ensures we discard the smaller height side.Final Answer:
The pointers should move based on height[left] < height[right], not > -> Option CQuick Check:
Move pointer of smaller height [OK]
- Using wrong comparison operator for pointer movement
- Including equal condition in loop causing extra iteration
- Using max height instead of min for area
Solution
Step 1: Understand the constraint
The container must include the first line at index 0, so left pointer stays fixed at 0.Step 2: Move right pointer from the end towards 0
Move right pointer from the last index towards 1, calculating area with fixed left pointer at 0, updating max area.Step 3: Avoid nested loops for efficiency
This approach keeps O(n) time by moving only one pointer, unlike nested loops which are O(n²).Final Answer:
Fix left pointer at 0 and move right pointer from end to start, calculating max area -> Option DQuick Check:
Fix one pointer, move other to find max area [OK]
- Moving wrong pointer fixed at 0
- Using nested loops causing slow performance
- Ignoring the fixed pointer constraint
Ready to prove it?
Pass the test and this pattern moves unknown
