Two-pointer algorithm visualization with pixel art sprites
Press → or Space to continue
Given the height of each terrain column...
Calculate how many water blocks get trapped.
heights = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
[1] Nothing to hold it. Zero.
[0, 1] No valley. Still zero.
[1, 0, 1] Two walls, one gap. One unit of water.
[1, 0, 2] Still one unit. The water can only rise to the shorter wall.
[1, 0, 2] Still one unit. The water can only rise to the shorter wall.