Dsa
Monotonic Stack
Warm-up
Water Trap Prediction
You are given a list of integers representing the height of buildings. Calculate the maximum rainwater that can be trapped between the buildings after a rain. The trapped water over each section of the buildings is the height of the taller building on the left minus the height of the current building, provided there is a taller or same height building on the right as well. Print the total trapped water.
Input Format:
- A single line containing N followed by N space-separated integers representing heights of buildings.
Output Format:
- A single integer representing the total trapped water.
Example:
Input:
8 1 0 2 1 0 1 3 2 1 2 1
Output:
6
Key concepts
monotonic_stackarray
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now