Complete the code to initialize the total water trapped variable.
let mut total_water = [1];We start counting trapped water from zero because initially no water is trapped.
Complete the code to update the left maximum height while iterating.
left_max = [1](left_max, height[left]);We update left_max to the maximum height seen so far on the left side.
Fix the error in the condition to decide which pointer to move.
if height[left] < [1] {
We compare height[left] with height[right] to decide pointer movement.
Fill both blanks to correctly calculate trapped water and move pointers.
if height[left] < height[right] { total_water += [1] - height[left]; left += [2]; } else { total_water += right_max - height[right]; right -= 1; }
We add trapped water by subtracting current height from left_max and move left pointer forward by 1.
Fill all three blanks to complete the trapping rain water function.
fn trap(height: Vec<i32>) -> i32 {
let (mut left, mut right) = (0, height.len() - 1);
let (mut left_max, mut right_max) = (0, 0);
let mut total_water = 0;
while left < right {
if height[left] < height[right] {
left_max = [1](left_max, height[left]);
total_water += left_max - height[left];
left += [2];
} else {
right_max = [3](right_max, height[right]);
total_water += right_max - height[right];
right -= 1;
}
}
total_water
}We update left_max and right_max with max function, move left pointer by 1.
