Complete the code to initialize the total trapped water to zero.
water = [1]We start counting trapped water from zero because initially no water is trapped.
Complete the code to find the maximum height to the left of the current bar.
left_max = max(height[:[1]])
We take all bars to the left of the current index i, so slicing up to i excludes the current bar.
Fix the error in the code to calculate trapped water at index i.
water += min(left_max, right_max) - [1]
We subtract the height of the current bar to find how much water can be trapped above it.
Fill both blanks to complete the loop that iterates over the bars except the first and last.
for i in range([1], [2]):
We skip the first and last bars because they cannot trap water.
Fill all three blanks to complete the function that calculates trapped rain water.
def trap(height): water = [1] for i in range([2], [3]): left_max = max(height[:i]) right_max = max(height[i+1:]) water += min(left_max, right_max) - height[i] return water
The function initializes water to zero, loops from the second bar to the second last bar, and calculates trapped water correctly.