You're tasked with transforming an array to make a specific sequence appear as a subsequence. Given a target array of distinct integers and an arr array that may contain duplicates, you need to find the minimum number of insertion operations required.
In each operation, you can insert any integer at any position in arr. For example, if arr = [1,4,1,2], you can insert 3 to get [1,4,3,1,2].
Goal: Return the minimum operations needed to make target a subsequence of the modified arr.
Note: A subsequence maintains the relative order of elements but allows deletions. For example, [2,7,4] is a subsequence of [4,2,3,7,2,1,4], but [2,4,2] is not.
Input & Output
Visualization
Time & Space Complexity
For each of n target elements, we try m arr elements, with recursive depth of n
Memoization table size plus recursion stack depth
Constraints
- 1 โค target.length, arr.length โค 105
- 1 โค target[i], arr[i] โค 109
- target contains distinct integers
- arr can contain duplicate integers