FunctionRacerProblemsRace

Rising List From Spares

HardArraysPython, JavaScript, TypeScript, Java, C++

One move replaces one item of nums with a value taken from spares. A spare value is not used up: it may be put in as many places as you like.

Return the fewest moves after which nums is strictly increasing (every item larger than the one before it), or -1 if that cannot be done.

Example 1

nums [2,9,4,7]
spares [6,3]
returns 1

Replace the 9 with 3: the list becomes 2, 3, 4, 7.

Example 2

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

The 6 has to be followed by something larger, and the only spare, 5, does not help in either place.

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 Arrays problems