Container With Most Water in DSA Rust - Time & Space Complexity
We want to know how the time needed to find the container with the most water grows as the number of lines increases.
How does the number of steps change when we have more lines to check?
Analyze the time complexity of the following code snippet.
fn max_area(height: Vec<i32>) -> i32 {
let (mut left, mut right) = (0usize, height.len() - 1);
let mut max_area = 0;
while left < right {
let width = (right - left) as i32;
let h = if height[left] < height[right] { height[left] } else { height[right] };
max_area = std::cmp::max(max_area, width * h);
if height[left] < height[right] {
left += 1;
} else {
right -= 1;
}
}
max_area
}
This code finds the largest container area formed between two lines by moving pointers from both ends inward.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves two pointers inward checking areas.
- How many times: The loop runs at most once for each element, so about n times where n is the number of lines.
As the number of lines grows, the steps increase 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 grow directly with the number of lines, not faster or slower.
Time Complexity: O(n)
This means the time to find the largest container grows in a straight line with the number of lines.
[X] Wrong: "We must check every pair of lines, so it takes n squared steps."
[OK] Correct: The two-pointer method smartly skips unnecessary pairs, so it only needs to check each line once.
Understanding this time complexity shows you can spot efficient ways to solve problems, a skill valued in real coding challenges.
"What if we used a nested loop to check every pair instead of two pointers? 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 maximizing the area between them.Step 2: Identify what to calculate
The area is calculated by the distance between the 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 = max area between lines [OK]
- Confusing sum of heights with area
- Choosing tallest lines without considering distance
- Ignoring the smaller height between two lines
Solution
Step 1: Identify Rust vector syntax
In Rust, a vector is declared usingvec![...]macro to create a growable array.Step 2: Check each option
let heights = vec![1, 8, 6, 2, 5, 4, 8, 3, 7]; usesvec![...]correctly. let heights = [1, 8, 6, 2, 5, 4, 8, 3, 7]; is an array, not a vector. let heights = (1, 8, 6, 2, 5, 4, 8, 3, 7); is a tuple. let heights = {1, 8, 6, 2, 5, 4, 8, 3, 7}; uses braces which is invalid syntax.Final Answer:
let heights = vec![1, 8, 6, 2, 5, 4, 8, 3, 7]; -> Option BQuick Check:
Vector syntax = vec![...] [OK]
- Using array syntax instead of vector
- Using tuple parentheses instead of brackets
- Using braces which is invalid
fn max_area(height: Vec<i32>) -> i32 {
let mut left = 0;
let mut right = height.len() - 1;
let mut max_area = 0;
while left < right {
let width = (right - left) as i32;
let h = if height[left] < height[right] { height[left] } else { height[right] };
let area = width * h;
if area > max_area {
max_area = area;
}
if height[left] < height[right] {
left += 1;
} else {
right -= 1;
}
}
max_area
}
fn main() {
let heights = vec![1,8,6,2,5,4,8,3,7];
println!("{}", max_area(heights));
}Solution
Step 1: Understand the two-pointer approach
Start with left at 0 and right at last index, calculate area, move pointer with smaller height inward.Step 2: Trace the code with given heights
Max area found is between height 8 at index 1 and height 7 at index 8, width = 7, area = 7 * 7 = 49.Final Answer:
49 -> Option DQuick Check:
Max area = 49 [OK]
- Calculating area with max height instead of min
- Not updating max area correctly
- Moving wrong pointer in the loop
fn max_area(height: Vec<i32>) -> i32 {
let mut left = 0;
let mut right = height.len();
let mut max_area = 0;
while left < right {
let width = (right - left) as i32;
let h = if height[left] < height[right] { height[left] } else { height[right] };
let area = width * h;
if area > max_area {
max_area = area;
}
if height[left] < height[right] {
left += 1;
} else {
right -= 1;
}
}
max_area
}Solution
Step 1: Check initialization of right pointer
Rust vectors are zero-indexed, so last index is length - 1, not length.Step 2: Identify impact of wrong right pointer
Using length causes out-of-bounds access when accessing height[right], leading to runtime error.Final Answer:
Change right initialization to height.len() - 1 -> Option AQuick Check:
Right pointer = last index = len() - 1 [OK]
- Using length as index causing panic
- Changing loop condition incorrectly
- Using max height instead of min for area
Solution
Step 1: Understand the two-pointer approach
We move pointers inward and calculate area at each step, updating max area found.Step 2: Track indices when max area updates
Along with max_area, store the current left and right indices whenever a larger area is found.Final Answer:
Keep track of max_area and update a tuple (left_index, right_index) whenever max_area changes -> Option CQuick Check:
Track indices with max area updates [OK]
- Returning sum instead of area
- Using nested loops losing O(n) efficiency
- Returning only one index instead of both
Ready to prove it?
Pass the test and this pattern moves unknown
