Custom Cheese Cursor

Trapping Rain Water

Two-pointer algorithm visualization with pixel art sprites

Press or Space to continue

Loading Phaser...
Loading Phaser...
Loading Phaser...
💧 Total Water
6

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]
Loading Phaser...

Nothing to hold it. Zero.

[0, 1]
Loading Phaser...

No valley. Still zero.

[1, 0, 1]
Loading Phaser...
💧 Total Water
1

Two walls, one gap. One unit of water.

[1, 0, 2]
Loading Phaser...
💧 Total Water
1

Still one unit. The water can only rise to the shorter wall.

[1, 0, 2]
Loading Phaser...
💧 Total Water
4

Still one unit. The water can only rise to the shorter wall.

Loading Phaser...

🎮 Game State

L
0
R
11
leftMax
0
rightMax
1
Step 0
No steps yet...
Loading Phaser...

🎮 Game State

L
0
R
11
leftMax
0
rightMax
1
Step 0
No steps yet...
Loading Phaser...

🎮 Game State

L
0
R
10
leftMax
3
rightMax
2
Step 0
No steps yet...
(0-10)
Loading Phaser...

🎮 Game State

L
0
R
11
leftMax
0
rightMax
1
Step 0
No steps yet...