1713. Minimum Operations to Make a Subsequence
Description
You are given an array target
that consists of distinct integers and another integer array arr
that can have duplicates.
In one operation, you can insert any integer at any position in arr
. For example, if arr = [1,4,1,2]
, you can add 3
in the middle and make it [1,4,3,1,2]
. Note that you can insert the integer at the very beginning or end of the array.
Return the minimum number of operations needed to make target
a subsequence of arr
.
A subsequence of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the remaining elements' relative order. For example, [2,7,4]
is a subsequence of [4,2,3,7,2,1,4]
(the underlined elements), while [2,4,2]
is not.
Example 1:
Input: target = [5,1,3], arr = [9,4,2,3,4] Output: 2 Explanation: You can add 5 and 1 in such a way that makes arr = [5,9,4,1,2,3,4], then target will be a subsequence of arr.
Example 2:
Input: target = [6,4,8,1,3,2], arr = [4,7,6,2,3,8,6,1] Output: 3
Constraints:
1 <= target.length, arr.length <= 105
1 <= target[i], arr[i] <= 109
target
contains no duplicates.
Solutions
Solution 1: Longest Increasing Subsequence + Binary Indexed Tree
According to the problem statement, the longer the common subsequence between target
and arr
, the fewer elements need to be added. Therefore, the minimum number of elements to be added equals the length of target
minus the length of the longest common subsequence between target
and arr
.
However, the time complexity of finding the longest common subsequence is $O(m \times n)$, which cannot pass this problem. We need to change our approach.
We can use a hash table to record the index of each element in the target
array, then iterate through the arr
array. For each element in the arr
array, if the hash table contains that element, we add the index of that element to an array. This gives us a new array nums
, which represents the indices in the target
array of elements from arr
(excluding elements not in target
). The length of the longest increasing subsequence of this array nums
is the length of the longest common subsequence between target
and arr
.
Therefore, the problem is transformed into finding the length of the longest increasing subsequence of the nums
array. Refer to 300. Longest Increasing Subsequence.
The time complexity is $O(n \times \log m)$, and the space complexity is $O(m)$. Here, $m$ and $n$ are the lengths of target
and arr
, respectively.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
1 2 3 4 5 6 7 8 9 10< |