Trap rain water
Return the volume of water trapped between nonnegative unit-width bars. Empty input returns zero. Use constant auxiliary space.
left_wall, right_wall, height = 3, 5, 1
print(max(0, min(left_wall, right_wall)-height))Reason about the state
Process the side with the smaller boundary maximum. That side already has a sufficient opposite wall, so its trapped water is determined without future information. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return the volume of water trapped between nonnegative unit-width bars. Empty input returns zero. Use constant auxiliary space.
The explanation opens here once every check passes.