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 makesarr= [5,9,4,1,2,3,4], then target will be a subsequence ofarr.
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 <= 1051 <= target[i], arr[i] <= 109target contains no duplicates.Problem Overview: You are given two arrays: target (with distinct values) and arr. The task is to compute the minimum number of insert operations needed so that target becomes a subsequence of arr. Instead of simulating insertions directly, the key observation is that maximizing the part of target already appearing in order inside arr minimizes the required operations.
Approach 1: Dynamic Programming (O(n * m) time, O(n * m) space)
This approach treats the problem as a variation of the Longest Common Subsequence (LCS). Compute the LCS between target and arr using a DP table where dp[i][j] represents the LCS length for the first i elements of target and first j elements of arr. If elements match, extend the subsequence; otherwise take the maximum of skipping one element from either array. The minimum operations required equals target.length - LCS. While straightforward and useful for understanding subsequence alignment, the O(n * m) time complexity becomes too slow for large inputs.
Approach 2: Hash Map + Longest Increasing Subsequence (O(n log n) time, O(n) space)
The optimal solution converts the problem into a Longest Increasing Subsequence (LIS) problem. Since target contains unique values, create a hash map mapping each value to its index in target. Then iterate through arr and replace each element with its mapped index if it exists in the map. This produces a sequence of indices representing the order of elements relative to target.
The goal becomes finding the longest increasing subsequence of these indices. An increasing sequence means elements appear in the correct order relative to target. Compute LIS efficiently using binary search and a dynamic array that stores the smallest tail value for subsequences of each length. Each index either extends the sequence or replaces a larger tail via binary search.
If the LIS length is k, then k elements of target already appear in correct order inside arr. The remaining target.length - k elements must be inserted. This reduces the problem to a classic binary search optimization combined with hash table indexing and sequence processing over an array.
Recommended for interviews: The hash map + LIS approach is what most interviewers expect. It demonstrates the ability to transform a subsequence matching problem into an index-ordering problem and apply the O(n log n) LIS technique. The dynamic programming solution shows understanding of subsequences but does not scale well for the input constraints.
This approach uses a hash map (or dictionary) to map each element of the target to its index. We then convert the problem into finding the longest increasing subsequence in terms of the indices in the target array. By doing so, the number of insertions required corresponds to the difference between the target array length and the length of this subsequence.
The C code creates a position map (pos) to store indexes of target elements. It uses a list lis to track the indices of elements in arr present in target, forming the longest increasing subsequence. The bisect_left function is used to find the insertion point in the lis, similar to the binary search approach of finding the increasing subsequence. The result is calculated by subtracting the length of lis from the length of target.
The solution has a time complexity of O(n log n), where n is the length of arr. The space complexity is O(n) due to the use of the lis list and the position map.
This approach uses dynamic programming to directly compute the operations necessary to form target directly. It keeps track of the length of the longest subsequence that can be achieved up to each position in arr, using knowledge of target positioning.
The C solution uses a dynamic programming array dp to keep track of the maximum subsequence length that can be achieved at every point in arr. The algorithm iterates over arr, updating dp based on whether elements match between target and arr. The result is derived by subtracting the length of the longest valid subsequence from the length of target.
The solution provides a time complexity of O(n * m) and a space complexity of O(m), where n is the size of arr and m is the size of target. It balances direct comparison with memory efficiency.
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.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Using Hash Map and Longest Increasing Subsequence (LIS) | The solution has a time complexity of O(n log n), where n is the length of |
| Using Dynamic Programming Approach | The solution provides a time complexity of O(n * m) and a space complexity of O(m), where n is the size of |
| Longest Increasing Subsequence + Binary Indexed Tree | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dynamic Programming (LCS) | O(n * m) | O(n * m) | Good for learning subsequence matching or when constraints are small |
| Hash Map + Longest Increasing Subsequence | O(n log n) | O(n) | Optimal for large inputs; converts order matching into LIS using binary search |
【LCS/LIS】花花酱 LeetCode 1713. Minimum Operations to Make a Subsequence - 刷题找工作 EP379 • Hua Hua • 1,649 views views
Watch 6 more video solutions →Practice Minimum Operations to Make a Subsequence with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor