Dsa
Monotonic Stack
Hard
Building Visibility
Given the heights of n buildings in a line, compute how many buildings are visible from each building's perspective. A building is visible from another if it is to the right and is taller than all previous buildings between them.
Input Format:
- The first line contains an integer n (1 <= n <= 10^5), the number of buildings.
- The second line contains n space-separated integers, representing the heights of the buildings.
Output Format:
- Output a list of n integers, where the ith integer indicates how many buildings are visible when viewed from the ith building.
Example:
Input:
5
1 3 2 4 1
Output:
4 1 1 0 0
Key concepts
monotonic_stackbuildingssight
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