1
var lengthOfLIS = function (A) {2
if (!A.length) return 0;3
let tails = [-Infinity]; //here -Infinity acts as a Sentinel, for cleaner code (or else I would ahve to place A[0] here)5
for (let i = 0; i < A.length; i++) {6
// If my curr element is bigger than all possible tails, i just need to create a new subarray, which of course will be of length tails.length-1 +17
if (A[i] > tails[tails.length - 1]) tails.push(A[i]);10
hi = tails.length - 1;11
//binary search to find where to place my current element so i have more chances of creating a bigger subarray13
let mid = (lo + hi) >> 1;14
if (tails[mid] < A[i]) lo = mid + 1;20
return tails.length - 1; // is the length of the longest possible subarray (-1 because of the -Infinity I added)22
var minOperations = function (T, A) {23
let seen = new Set(T),27
//filter out all the unnecessary values28
A = A.filter((d) => seen.has(d));29
for (let i = 0; i < T.length; i++) originalIndex[T[i]] = i; //maintain the original indices of the values in T30
A = A.map((d) => originalIndex[d]);31
return T.length - lengthOfLIS(A);