An entry queue scans n badges in arrival order, each with an integer priority. A non-falling run is a subsequence of badges (not necessarily adjacent), in arrival order, whose priorities never decrease (each chosen priority is greater than or equal to the previous chosen one). Among all non-falling runs, consider only the longest ones. Report how many longest non-falling runs exist. Two runs are different if they use different sets of positions, even when the priority values coincide.
Line 1: an integer n.
Line 2: n space-separated integers, the priorities in arrival order.
A single integer: the number of longest non-decreasing subsequences.
Example 1
Input
5 1 2 2 3 3
Expected
1
Explanation
The longest non-falling run has length 5 and uses all badges, so there is exactly 1 longest run.
Example 2
Input
4 2 1 2 1
Expected
3
Explanation
The longest non-falling runs have length 2: positions (1,3) giving 2,2 and (2,4) giving 1,1 and (2,3) giving 1,2. There are 3 of them.
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 →