Using the Binary search variant of LIS will give you a better Time complexity. Here's the code :
var maxEnvelopes = function(envelopes) {
envelopes.sort((a, b) => a[0] === b[0] ? b[1] - a[1] : a[0] - b[0]);
const n = envelopes.length;
const sub = [envelopes[0][1]];
function binarySearch(sub, num) {
let left = 0, right = sub.length;
while (left < right) {
let mid = Math.floor((left + right) / 2);
if (num > sub[mid]) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
for (let i = 1; i < n; i++) {
const num = envelopes[i][1];
if (num > sub[sub.length - 1]) {
sub.push(num);
} else {
const x = binarySearch(sub, num);
sub[x] = num;
}
}
return sub.length;
};
// Example usage:
// const envelopes = [[5,4],[6,4],[6,7],[2,3]];
// console.log(maxEnvelopes(envelopes)); // Output: 3