Skip to main content

Hard two pointers · arrays

Rain on the rooftops

Rain falls on a row of buildings. How much water is left sitting between them once it stops.

Write trapWater(heights): heights are the building heights, left to right, each one unit wide. Return the total units of water trapped between them after rain.

Keep level, a copy of heights where each spot you have settled is raised to its water line. Call show(level) after each step and the stage fills the gaps as bars.

Example: trapWater([4, 2, 0, 3, 2, 5]) returns 9.

Tests you can see

  • trapWater([0,1,0,2,1,0,1,3,2,1,2,1])6
  • trapWater([4,2,0,3,2,5])9

More tests are hidden. The server runs them all before a time counts.

Call show() while your code works and the run plays back here.

javascripttrapWater()

Tab indents · Cmd/Ctrl+Enter runs