Container With Most Water in DSA Python - Time & Space Complexity
We want to understand how the time needed to find the container with the most water changes as the number of lines grows.
How does the number of steps grow when we have more lines to check?
Analyze the time complexity of the following code snippet.
def max_area(height):
left, right = 0, len(height) - 1
max_water = 0
while left < right:
width = right - left
current_height = min(height[left], height[right])
max_water = max(max_water, width * current_height)
if height[left] < height[right]:
left += 1
else:
right -= 1
return max_water
This code finds the maximum water container by moving two pointers from both ends towards the center.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves the left and right pointers towards each other.
- How many times: At most, it runs once for each element from both ends, so about n times where n is the number of lines.
As the number of lines increases, the number of steps grows roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The steps increase directly with the number of lines, so doubling the lines roughly doubles the work.
Time Complexity: O(n)
This means the time to find the max water grows in a straight line with the number of lines.
[X] Wrong: "Because there are two pointers, the time is O(n²) since we check pairs."
[OK] Correct: The pointers only move inward once each step, so total steps are just n, not n times n.
Understanding this linear time approach shows you can find efficient solutions by smartly moving pointers instead of checking all pairs.
"What if we used a nested loop to check every pair of lines? How would the time complexity change?"
Practice
Container With Most Water ask you to find given an array of heights?Solution
Step 1: Understand the problem statement
The problem asks to find two lines that together with the x-axis form a container holding the most water, which means the maximum area between two vertical lines.Step 2: Identify what to calculate
We calculate the area as the width between two lines multiplied by the smaller height of the two lines.Final Answer:
The maximum area formed between two vertical lines and the x-axis -> Option AQuick Check:
Container with most water = maximum area [OK]
- Confusing area with sum of heights
- Looking for minimum height only
- Counting lines instead of calculating area
Container With Most Water problem in Python?Solution
Step 1: Understand pointer initialization
We start with two pointers at the edges of the array: left at 0 and right at the last index, which is length minus one.Step 2: Check each option
left, right = 0, len(height) - 1 correctly sets left to 0 and right to len(height) - 1. Others either start at wrong indices or exceed array bounds.Final Answer:
left, right = 0, len(height) - 1 -> Option BQuick Check:
Start pointers at array ends = left, right = 0, len(height) - 1 [OK]
- Setting right pointer to len(height) (out of range)
- Starting left pointer at 1 instead of 0
- Swapping left and right initialization
Container With Most Water problem?def max_area(height):
left, right = 0, len(height) - 1
max_area = 0
while left < right:
width = right - left
area = min(height[left], height[right]) * width
max_area = max(max_area, area)
if height[left] < height[right]:
left += 1
else:
right -= 1
return max_area
print(max_area([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. Move left pointer since height[left]Step 2: Continue moving pointers and update max_area
Next left=1 (8), right=8 (7), area = min(8,7)*7=49 (max_area updated). Then right moves left, area decreases. Max area remains 49.Final Answer:
49 -> Option AQuick Check:
Max area calculated = 49 [OK]
- Calculating area with max height instead of min
- Not updating max_area correctly
- Moving wrong pointer in the loop
Container With Most Water and select the fix:def max_area(height):
left, right = 0, len(height) - 1
max_area = 0
while left <= right:
width = right - left
area = min(height[left], height[right]) * width
max_area = max(max_area, area)
if height[left] < height[right]:
left += 1
else:
right -= 1
return max_areaSolution
Step 1: Analyze the loop condition
The loop uses 'while left <= right', which allows left and right to be equal, causing width to be zero and area zero, which is unnecessary.Step 2: Correct the loop condition
Changing to 'while left < right' ensures pointers do not cross or become equal, avoiding zero width and redundant calculations.Final Answer:
Change while condition to 'left < right' -> Option DQuick Check:
Loop until pointers cross, not equal [OK]
- Using <= causes zero width calculation
- Using max instead of min for height
- Moving both pointers at once
[4,3,2,1,4], what is the maximum water container area and which pair of lines form it?Solution
Step 1: Calculate area between lines at indices 0 and 4
Height = min(4,4) = 4, width = 4 - 0 = 4, area = 4 * 4 = 16.Step 2: Check other pairs for larger area
Pairs (1,4): min(3,4)*3=9; (0,3): min(4,1)*3=3; (2,4): min(2,4)*2=4. None exceed 16.Final Answer:
16, formed by lines at indices 0 and 4 -> Option CQuick Check:
Max area = 16 from edges [OK]
- Ignoring width when calculating area
- Using max height instead of min
- Not checking all pairs properly
Ready to prove it?
Pass the test and this pattern moves unknown
