Skip to content
RSS feed tkhwang on GitHub tkhwang on Twitter

πŸ€– leetcode 1713. Minimum Operations to Make a Subsequence | hard | dynamic-programming | javascript

πŸ—’οΈ Problems

1713. Minimum Operations to Make a Subsequence - Leetcode

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.
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.

πŸ€” Understand problem

  • Make the sub-sequence of arr equal to target.
  • First, find the LCS (Longest Common Subsequence).
  • Add the differences.

πŸ€¦β€β™‚οΈ First attempt

var minOperations = function (target, arr) {
  const NT = target.length;
  const N = arr.length;

  // dp[i][j] : target[0:i-1] arr[0:j-1]
  const dp = Array(NT + 1)
    .fill(null)
    .map((_) => Array(N + 1).fill(0));

  for (let i = 1; i < NT + 1; i += 1) {
    for (let j = 1; j < N + 1; j += 1) {
      if (target[i - 1] === arr[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }

  return NT - dp[NT][N];
};

πŸ™…β€β™‚οΈ Failed time complexity : O(n^2)

  • It fails due to the heap overflow.

πŸ₯³ Think differently

  • Is it possible to reduce the time complexity to O(n logn) for finding the longest common subsequence ?

πŸ’‘ Change LCS to LIS

✨ Convert arr to index in target

const obj = {};
for (const [i, num] of target.entries()) {
  obj[num] = i;
}
const candidates = [];

for (const num of arr) {
  if (obj[num] === undefined) continue;
  candidates.push(obj[num]);
}

πŸ’‘ Find LIS (Longest Increasing Subsequence)

const bisectLeft = (array, target, left = 0, right = array.length) => {
  // [left, right) half inclusive range

  while (left < right) {
    const mid = Math.floor((left + right) / 2);

    // left-most
    if (array[mid] >= target) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return left;
};

const piles = [];
for (const num of candidates) {
  const index = bisectLeft(piles, num);
  if (index === piles.length) {
    piles.push(num);
  } else {
    piles[index] = num;
  }
}

const count = piles.length;

The answer

The answer (the number of minimum insertions) is the number of unmatched characters

// target.length - commonSubSequence.length
return target.length - count;

πŸ”₯ My Solution

/**
 * @param {number[]} target
 * @param {number[]} arr
 * @return {number}
 */
var minOperations = function (target, arr) {
  const obj = {};
  for (const [i, num] of target.entries()) {
    obj[num] = i;
  }
  const candidates = [];

  for (const num of arr) {
    if (obj[num] === undefined) continue;
    candidates.push(obj[num]);
  }

  const bisectLeft = (array, target, left = 0, right = array.length) => {
    // [left, right) half inclusive range

    while (left < right) {
      const mid = Math.floor((left + right) / 2);

      // left-most
      if (array[mid] >= target) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
    return left;
  };

  const piles = [];
  for (const num of candidates) {
    const index = bisectLeft(piles, num);
    if (index === piles.length) {
      piles.push(num);
    } else {
      piles[index] = num;
    }
  }

  const count = piles.length;
  return target.length - count;
};

πŸ™†β€β™‚οΈ Time complexity: O(n logn)