Rain trapped between bars
heights[i] is the height of a bar of width 1, and the bars stand side by side with no gaps. Rain fills every dip between them up to where it would spill over, and water above either end runs off. Return how many units of water stay trapped. Heights are non-negative integers.
Aim for O(n) time and O(1) extra space.
Besides the tests, one large input (8,000 bars) with a ~200 ms limit (scaled to your device's speed): brute force won't pass.
Examples
trapped_rain([3,0,2,0,4])→7trapped_rain([0,2,0,3,1,0,1,3,2,1,2,1])→10trapped_rain([4,1,2,5,0,3])→8