SalesforceOA #43
Visible Towers
Estimated MediumJudge-ready
Topics (AI-labelled)Stacks & Heaps
Towers stand in a row. Tower x is visible from tower y if and only if every tower between them is strictly shorter than the target tower x. The observer y's own height imposes no restriction. For each tower, output the total number of towers it can see to its left and right, excluding itself.
Input
The input gives n and n heights. The original has no numeric bounds; this site adds 1≤n≤200000, 0≤height[i]≤10^9. Duplicate heights are allowed.
Output
Output the n visible-tower counts in original position order, separated by spaces or newlines.
Example 1
Input:
4
5 2 10 1
Output:
2 2 3 1
Example 2
Input:
3
1 2 3
Output:
2 2 1
Example 3
Input:
1
7
Output:
0
Example notes
The outputs of the three public examples are, in order: 2 2 3 1; 2 2 1; 0. They have been checked by independent enumeration or direct simulation.
Editorial
Loading solution…