A row of n towers is given left to right, each with a positive integer height. The sun sets far to the east (past the right end of the row). A tower can see the sunset if and only if it is strictly taller than every tower to its right (a tower with nothing to its right always can).
Count how many towers can see the sunset.
Line 1: an integer n.
Line 2: n space-separated integers, the tower heights from left (west) to right (east).
A single integer: the number of towers that can see the sunset.
Example 1
Input
6 4 2 3 1 5 3
Expected
2
Explanation
Scanning from the right: the last tower 3 sees the sunset; 5 is taller than everything to its right so it sees it; 1, 3, 2, 4 are all blocked by the 5. Two towers can see the sunset.
Example 2
Input
4 1 2 3 4
Expected
1
Explanation
Only the tallest, rightmost tower (4) is strictly taller than everything to its right; every other tower is blocked. Exactly 1 tower sees the sunset.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →