Rising List From Spares
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
1 <= len(nums) <= 500 <= len(spares) <= 500 <= nums[i], spares[i] <= 1000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Arrays problems
- Slide K Ones TogetherHard
- Spread of Every SelectionHard
- Swaps for Two Rising ListsHard
- Two Trades at MostHard
- Build Array from PermutationBeginner
- Close NeighborsBeginner
- Concatenation of ArrayBeginner
- Deal Into PilesBeginner