Make Array Strictly Increasing - Problem
Given two integer arrays arr1 and arr2, return the minimum number of operations (possibly zero) needed to make arr1 strictly increasing.
In one operation, you can choose two indices 0 <= i < arr1.length and 0 <= j < arr2.length and do the assignment arr1[i] = arr2[j].
If there is no way to make arr1 strictly increasing, return -1.
Input & Output
Example 1 — Basic Replacement
$
Input:
arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]
›
Output:
1
💡 Note:
Replace arr1[1] = 5 with arr2[1] = 3, then arr1 = [1,3,3,6,7]. Replace arr1[2] = 3 with arr2[3] = 4, then arr1 = [1,3,4,6,7] which is strictly increasing. Total operations: 1.
Example 2 — No Operations Needed
$
Input:
arr1 = [1,2,3,4], arr2 = [1,3,5,7]
›
Output:
0
💡 Note:
arr1 is already strictly increasing [1,2,3,4], so no operations are needed.
Example 3 — Impossible Case
$
Input:
arr1 = [1,5,3,6,7], arr2 = [4,3,1]
›
Output:
-1
💡 Note:
There is no way to make arr1 strictly increasing. We cannot find suitable replacements to fix the decreasing subsequence.
Constraints
- 1 ≤ arr1.length, arr2.length ≤ 2000
- 0 ≤ arr1[i], arr2[i] ≤ 109
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code