Tower View
InterviewTime limit1.5sMemory limit1024 MB
For each building, count the buildings visible to its left and right, where a shorter or equal building is hidden behind a taller one, and report the nearest visible index.
- Level
Medium7 of 10
- Topics
- Stack, Array, Implementation, Two pointers
- Solved
- No attempts yet
Problem
There are buildings of various heights on a straight line. From the roof of each building, you want to know how many sides of buildings are visible on either side.
For the -th building, buildings , , ..., are on the left, and buildings , , ..., are on the right. The distance between adjacent buildings is the same.
Suppose the current building has height . Only buildings with height greater than are visible.
In the direction you are looking, if a building of height at most lies behind a building of height , it is hidden and not visible.
Find which buildings are visible from each building.
Input
The first line gives the number of buildings .
The second line gives the heights of the buildings, separated by spaces.
Output
For each building , output the number of visible buildings.
If the number of visible buildings is at least 1, also output the smallest index among the visible buildings closest to building .