Make Array Strictly Increasing

HardDynamic Programming1d dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given two integer arrays arr1 and arr2. In one operation you choose an index i of arr1 and an index j of arr2 and set arr1[i] = arr2[j]. Elements of arr2 are not consumed, so the same value may be used many times. Return the minimum number of operations needed to make arr1 **strictly increasing**. If it cannot be done, return -1.

Examples

Input: arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]
Output: 1
Replace 5 with 2: [1,2,3,6,7].
Input: arr1 = [1,5,3,6,7], arr2 = [4,3,1]
Output: 2
Replace 5 with 3 and 3 with 4: [1,3,4,6,7].
Input: arr1 = [1,5,3,6,7], arr2 = [1,6,3,3]
Output: -1
No sequence of replacements makes the array strictly increasing.

Constraints

  • 1 <= arr1.length, arr2.length <= 2000
  • 0 <= arr1[i], arr2[i] <= 10^9

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More 1d dp problems