Interview Questions/Coding/Storm Drain Skyline Audit

Storm Drain Skyline Audit

Preview mode. Log in to edit, run, submit, and save progress.

Hard

Storm Drain Skyline Audit

A city platform stores a skyline scan after a storm. Each value in walls represents the height of one wall segment from left to right. Water may rest above a segment only if there is a boundary somewhere to its left and a boundary somewhere to its right. The amount of water held above a segment depends on the lower of the best boundary on both sides. Segments at the edges may hold no water unless the surrounding boundaries make it possible. Input Format: You are given an integer array walls. Output Format: Return the total number of water units trapped after the storm settles.

Examples

Example 1
Input:
walls = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
Output:
6

Explanation: The trapped water above each index is [0, 0, 1, 0, 1, 2, 1, 0, 0, 1, 0, 0]. Their sum is 6, so the output is 6.

Approach hint

Look for the state that actually matters after each operation.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp