Ready
Elevation Map
Left Max (lmax)
Right Max (rmax)
Operation: -

Current Phase

-

Current Index (i)

-

Left Max

-

Right Max

-

Total Water

0
Click "Step" or "Play" to start the visualization
Elevation
Current Index
Left Max
Right Max
Trapped Water
Final Result

Python Code

n = len(height)
lmax = [0] * n
rmax = [0] * n
lmax[0] = height[0]
rmax[n-1] = height[n-1]
for i in range(1, n):
    lmax[i] = max(lmax[i-1], height[i])
for i in range(n-2, -1, -1):
    rmax[i] = max(rmax[i+1], height[i])
out = 0
for i in range(n):
    out += min(lmax[i], rmax[i]) - height[i]
return out