In a faraway kingdom, a tower defense game is played using a series of towers lined up in a row. Each tower has a certain height, and the objective is to find the nearest taller tower to the right for each tower. If there is no such tower, you should consider its height as 0.
Given the heights of
Your task is to implement a function that uses a monotonic stack to efficiently compute the solution.
Line 1: KaTeX can only parse string typed expression — the number of towers.
Line 2: KaTeX can only parse string typed expression space-separated integers representing the heights of the towers KaTeX can only parse string typed expression.
Print KaTeX can only parse string typed expression space-separated integers, where the KaTeX can only parse string typed expression integer is the height of the nearest taller tower to the right of the KaTeX can only parse string typed expression tower, or 0 if there is no such tower.
5Output
3 7 8 3 6
7 8 0 6 0Explanation
For tower 1 with height 3, the nearest taller tower is tower 2 with height 7.
For tower 2 with height 7, the nearest taller tower is tower 3 with height 8.
For tower 3 with height 8, there is no taller tower to the right, so the value is 0.
For tower 4 with height 3, the nearest taller tower is tower 5 with height 6.
For tower 5 with height 6, there is no taller tower to the right, so the value is 0.