Skip to content

Latest commit

 

History

History

1713

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

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.

Companies:
Google

Related Topics:
Array, Hash Table, Binary Search, Greedy

Solution 1. LCS to LIS

The problem is the same as finding the longest common subsequence (LCS) between T and A. But the LCS DP solution takes O(TA) time and will get TLE.

To improve it, we can use the constraint that T contains no duplicate. So T tells us the arrangement of the numbers. We can replace each element in A with the index it appeared in T. Then the problem becomes finding the longest increasing subseqence (LIS) which can be done within O(AlogA) time.

Time Complexity

Generating the index map takes O(T) time.

Generating the idx array takes O(A) time.

LIS part takes O(AlogA) time.

So overall it's O(T + AlogA).

// OJ: https://leetcode.com/problems/minimum-operations-to-make-a-subsequence/
// Author: github.com/lzl124631x
// Time: O(T + AlogA)
// Space: O(T + A)
class Solution {
public:
    int minOperations(vector<int>& T, vector<int>& A) {
        unordered_map<int, int> m;
        for (int i = 0; i < T.size(); ++i) m[T[i]] = i;
        vector<int> idx, lis;
        for (int n : A) {
            if (m.count(n)) idx.push_back(m[n]);
        }
        for (int n : idx) {
            int i = lower_bound(begin(lis), end(lis), n) - begin(lis);
            if (i == lis.size()) lis.push_back(n);
            else lis[i] = n;
        }
        return T.size() - lis.size();
    }
};

Solution 2. LCS to LIS

For each T[i], we can find a set S of indexes such that A[j] = T[i] where j is in S.

For example:

T = [6,4,8,1,3,2], A = [4,7,6,2,3,8,6,1]
6 -> [2, 6]
4 -> [0]
8 -> [5]
1 -> [7]
3 -> [4]
2 -> [3]

Now we want to find a longest increasing sequence in these indexes. The LIS in the example is [2, 5, 7], which means [6, 8, 1] is a LCS between T and A.

We can reverse each array and concatenate them together and compute the LIS of the resulting array. In the above example, the concatenated array is [6, 2, 0, 5, 7, 4, 3].

Time Complexity

Generating the index map takes O(T) time.

Generating idx and merged arrays takes O(A) time.

LIS part takes O(AlogA) time.

So overall it's O(T + AlogA).

// OJ: https://leetcode.com/problems/minimum-operations-to-make-a-subsequence/
// Author: github.com/lzl124631x
// Time: O(T + AlogA)
// Space: O(T + A)
class Solution {
public:
    int minOperations(vector<int>& T, vector<int>& A) {
        unordered_map<int, int> m;
        for (int i = 0; i < T.size(); ++i) m[T[i]] = i;
        vector<vector<int>> idx(T.size());
        for (int i = A.size() - 1; i >= 0; --i) {
            if (m.count(A[i])) idx[m[A[i]]].push_back(i);
        }
        vector<int> merged, lis;
        for (auto &v : idx) {
            for (int n : v) merged.push_back(n);
        }
        for (int n : merged) {
            int i = lower_bound(begin(lis), end(lis), n) - begin(lis);
            if (i == lis.size()) lis.push_back(n);
            else lis[i] = n;
        }
        return T.size() - lis.size();
    }
};