Ctrl K

Strict Climb From Spare Parts

Hard
You are given a row of numbers row and a pool of spare numbers spares. One replacement takes any element of row and overwrites it with any number from spares. Spares are never used up: the same one can be taken as often as you like. The goal is to make row strictly increasing, with every number larger than the one before it. Return the minimum number of replacements (possibly 0). If it cannot be done, return -1.
Example 1
row=[2, 9, 4, 8], spares=[5, 3, 1] → 1

Replace 9 with the spare 3: [2, 3, 4, 8] is strictly increasing. One replacement is enough.

Example 2
row=[6, 5, 4], spares=[7, 1, 8] → 2

Put 1 in place of 6 and 7 in place of 4 to get [1, 5, 7]. A single replacement is not enough.

Example 3
row=[3, 3, 3], spares=[3] → -1

Every number in the row and among the spares is 3, so no strictly increasing row of three numbers can be built.

Constraints
  • 1 ≤ len(row) ≤ 2000
  • 1 ≤ len(spares) ≤ 2000
  • 0 ≤ row[i], spares[j] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBoxes Inside Boxes HardBest-Paying Set of Gigs HardThe Best-Paying Gigs HardClimbing Along Rows and Columns HardWords Hidden in a Text Medium
loading Python…
Input
row=[2, 9, 4, 8], spares=[5, 3, 1]
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 147 tests on the server, hidden ones included.