Rainwater Held by a Skyline
Problem
A city skyline is described by the DataFrame `skyline`, where each row gives a `position` (left-to-right index) and the `wall_m` value -- the metres of a unit-wide rooftop ledge at that position. After a storm, water pools on the rooftops: at any position the standing water level equals the shorter of the tallest ledge to its left (inclusive) and the tallest ledge to its right (inclusive); the water depth there is that level minus the position's own `wall_m`. Return a DataFrame with a single column `pooled_water` holding the total volume of water pooled across the whole skyline.
Input data
Example rows — the live problem includes the full dataset.
| position | wall_m |
|---|---|
| 1 | 0 |
| 2 | 1 |
| 3 | 0 |
| 4 | 2 |
| 5 | 1 |
Expected output
Your answer should return 1 row with the columns pooled_water.
Starter code (Pandas (Python))
import pandas as pd
def pooled_rainwater(skyline) -> pd.DataFrame:
# Your code here
return skylineSolve this Pandas question free
Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock