1
const binarySearch = (arr, target) => {
2
let left = 0;
3
let right = arr.length - 1;
4

5
while (left <= right) {
6
const mid = Math.floor((left + right) / 2);
7
if (arr[mid] === target) {
8
return mid;
9
}
10
if (arr[mid] < target) {
11
left = mid + 1;
12
} else {
13
right = mid - 1;
14
}
15
}
16

17
return left;
18
};
19

20
var maxEnvelopes = function (envelopes) {
21
envelopes.sort((a, b) => (a[0] === b[0] ? b[1] - a[1] : a[0] - b[0]));
22
const sub = [envelopes[0][1]];
23

24
for (let envelope of envelopes) {
25
if (envelope[1] > sub[sub.length - 1]) {
26
sub.push(envelope[1]);
27
} else {
28
const replaceIndex = binarySearch(sub, envelope[1]);
29
sub[replaceIndex] = envelope[1];
30
}
31
}
32

33
return sub.length;
34
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0