FunctionRacerProblemsRace

Second Larger Item to the Right

HardStacks and queuesPython, JavaScript, TypeScript, Java, C++

For every item of nums, look at the items to its right that are strictly larger than it, in the order they appear. Return a list with the second of them for every item, or -1 where there are fewer than two.

Example 1

nums [3,5,1,4,6]
returns [4,-1,6,-1,-1]

To the right of the 3 the larger items are 5, then 4, then 6: the second is 4. After the 5 only the 6 is larger. After the 1 come 4 and then 6.

Example 2

nums [7,7,7]
returns [-1,-1,-1]

Equal items are not larger, so nothing ever counts.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Stacks and queues problems