call(back)
Algorithms & Data StructureseasySign in to star problems

Pour Water II

Draw a terrain and the water resting on it as ASCII art. heights[i] is the ground height at column i, and water[i] is the surface height there once the water has settled (water[i] >= heights[i]).

printTerrain(heights, water) returns one row per level, from max(water) down to 1, joined by "\n": # for ground, ~ for water, and a space for air. Every row is as wide as the terrain — keep trailing spaces. An empty terrain prints the empty string.

printTerrain([2, 1, 1, 2, 1, 2, 2], [2, 2, 2, 3, 2, 2, 2])  ->  "   ~   \n#~~#~##\n#######"

   ~
#~~#~##
#######

Hints

Hint 1

Iterate levels from max(water) down to 1 and emit one character per column: ground if level ≤ heights[i], water if level ≤ water[i], otherwise a space.

Hint 2

Build each row with map + join, then join the rows with newlines — no trailing newline, and no trimming.

Asked at