Dsa
Monotonic Stack
Easy
Rainwater Storage
Given an array representing the height of vertical bars in a histogram, calculate how much water can be collected after rain. For each bar, the water it can trap is determined by the heights of the taller bars to the left and right. Write a program to compute this.
Input Format:
- A single line containing space-separated integers representing bar heights.
Output Format:
- A single integer representing the total amount of water that can be trapped.
Example:
Input:
0 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