Make Array Strictly Increasing Problem
Description
LeetCode Problem 1187.
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.
Example 1:
Input: arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]
Output: 1
Explanation: Replace 5 with 2, then arr1 = [1, 2, 3, 6, 7].
Example 2:
Input: arr1 = [1,5,3,6,7], arr2 = [4,3,1]
Output: 2
Explanation: Replace 5 with 3 and then replace 3 with 4. arr1 = [1, 3, 4, 6, 7].
Example 3:
Input: arr1 = [1,5,3,6,7], arr2 = [1,6,3,3]
Output: -1
Explanation: You can't make arr1 strictly increasing.
Constraints:
1 <= arr1.length, arr2.length <= 20000 <= arr1[i], arr2[i] <= 10^9
Solution
Approach: Dynamic Programming on the Kept Elements
Think about which elements of arr1 we keep. Add a sentinel -infinity at the front and +infinity at the end of arr1; they are never replaced. Let f[i] be the fewest replacements that make the array up to index i strictly increasing while keeping arr[i]. The answer is f of the last sentinel.
Only the set of values in arr2 matters, so sort it and remove duplicates.
To compute f[i], look at how many elements right before i were replaced, say k:
k = 0:arr[i - 1]is kept too, which works ifarr[i - 1] < arr[i]. Thenf[i] = f[i - 1].k >= 1: positionsi - k .. i - 1are replaced andarr[i - k - 1]is kept. The replaced values must be distinct, increasing and smaller thanarr[i], and it is best to make them as large as possible, which leaves the most room on the left. So they are theklargest values ofarr2belowarr[i]. Ifjis the number of values belowarr[i](found by binary search), the smallest of them isvalues[j - k], and the choice is valid whenarr[i - k - 1] < values[j - k]. Thenf[i]can bef[i - k - 1] + k.
k cannot exceed j (not enough values) or i - 1 (not enough elements before i). We can also stop once k >= f[i]: replacing k elements costs at least k, so it cannot beat the best found so far.
If the final value is still infinity, no choice works and the answer is -1.
Complexity: O(n * (log m + min(n, m))) time and O(n + m) space, where n and m are the lengths of arr1 and arr2, plus O(m log m) to sort arr2.
import bisect
class Solution:
def makeArrayIncreasing(self, arr1: List[int], arr2: List[int]) -> int:
INF = float("inf")
values = sorted(set(arr2)) # replacement values, sorted and without duplicates
# Sentinels: a -inf in front and a +inf at the end, which are never replaced.
arr = [-INF] + arr1 + [INF]
n = len(arr)
# f[i]: fewest replacements that make arr[0..i] strictly increasing with arr[i] kept.
f = [INF] * n
f[0] = 0
for i in range(1, n):
if arr[i - 1] < arr[i]:
f[i] = f[i - 1] # keep arr[i - 1] too
# Or replace the k elements just before i (k >= 1) with the k largest values below arr[i].
j = bisect.bisect_left(values, arr[i]) # values[:j] are the values below arr[i]
for k in range(1, min(i - 1, j) + 1):
if k >= f[i]:
break # more than f[i] replacements cannot win
if arr[i - k - 1] < values[j - k]:
f[i] = min(f[i], f[i - k - 1] + k)
return -1 if f[n - 1] == INF else f[n - 1]