Skip to main content

Rain Terraces

Definition​

The Rain Terraces Algorithm is a technique used to calculate the total volume of water that can be trapped between terraces formed by an elevation map

Practice​

rainTerraces(elevation_map):
left_max = 0
right_max = 0
water_volume = 0

for i from 0 to length(elevation_map) - 1:
left_max = max(left_max, elevation_map[i])

for i from length(elevation_map) - 1 to 0:
right_max = max(right_max, elevation_map[i])

for i from 0 to length(elevation_map) - 1:
water_volume += min(left_max, right_max) - elevation_map[i]
left_max = max(left_max, elevation_map[i])

return water_volume