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.

&nbsp;
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


&nbsp;
Constraints:


	1 &lt;= target.length, arr.length &lt;= 105
	1 &lt;= target[i], arr[i] &lt;= 109
	target contains no duplicates.


from bisect import bisect_left
class Solution:
    def minOperations(self, target: List[int], arr: List[int]) -> int:
        dt = {num: i for i, num in enumerate(target)}
        stack = []
        for num in arr:
            if num not in dt: continue
            i = bisect_left(stack, dt[num])
            if i == len(stack):
                stack.append(dt[num])
            else:
                stack[i] = dt[num]
        return len(target) - len(stack)
class Solution {
    public int minOperations(int[] target, int[] arr) {
        int n = target.length;
        Map<Integer, Integer> map = new HashMap<>();

        for(int i = 0; i < n; i++) {
            map.put(target[i], i);
        }

        List<Integer> array = new ArrayList<>();

        for(int i = 0; i < arr.length; i++) {
            if(!map.containsKey(arr[i])) {
                continue;
            }

            array.add(map.get(arr[i]));
        }

        int maxLen = 0;
        int[] tails = new int[n + 1];

        for(int i = 0; i < n; i++) {
            tails[i] = -1;
        }

        for(int num: array) {
            int index = findMinIndex(tails, maxLen, num);

            if(tails[index] == -1) {
                maxLen++;
            }
            tails[index] = num;
        }

        return n - maxLen;
    }

    public int findMinIndex(int[] tails, int n, int val) {
        int low = 0;
        int ans = n;
        int high = n - 1;

        while(low <= high) {
            int mid = (high + low) / 2;

            if(tails[mid] >= val) {
                ans = mid;
                high = mid - 1;
            }
            else {
                low = mid + 1;
            }
        }
        return ans;
    }
}
class Solution {
public:
    vector<int> ans;
    
    void bin(int lo, int hi, int num) {
        if(lo == hi) {
            ans[lo] = num;
            return;
        }
        int mid = (lo + hi) / 2;
        if(ans[mid] < num) bin(mid + 1, hi, num);
        else bin(lo, mid, num);
    }
    
    int minOperations(vector<int>& target, vector<int>& arr) {
        unordered_map<int, int> idx;
        for(int i = 0; i < target.size(); i++) {
            idx[target[i]] = i;
        }
        
        for(int i = 0; i < arr.size(); i++) {
            if(idx.find(arr[i]) == idx.end()) continue;
            int num = idx[arr[i]];
            if(ans.size() == 0 || num > ans.back()) {
                ans.push_back(num);
            }
            else {
                bin(0, ans.size() - 1, num);
            }
        }
        
        return (target.size() - ans.size());
    }
};
const minOperations = function (targets, arr) {
  const map = {};
  const n = targets.length;
  const m = arr.length;
  targets.forEach((target, i) => (map[target] = i));

//map elements in arr to index found in targets array
  const arrIs = arr.map(el => {
    if (el in map) {
      return map[el];
    } else {
      return -1;
    }
  });

//create a LIS table dp whose length is the longest increasing subsequence
  const dp = [];

  for (let i = 0; i < m; i++) {
    const curr = arrIs[i];
    if (curr === -1) continue;
    if (!dp.length || curr > dp[dp.length - 1]) {
      dp.push(curr);
    } else if (curr < dp[0]) {
      dp[0] = curr;
    } else {
      let l = 0;
      let r = dp.length;
      while (l < r) {
        const mid = Math.floor((l + r) / 2);
        if (arrIs[i] <= dp[mid]) {
          r = mid;
        } else {
          l = mid + 1;
        }
      }
      dp[r] = curr;
    }
  }
  return n-dp.length;
};
Minimum Operations to Make a Subsequence