Most Beautiful Item for Each Query LeetCode solution in JavaScript

1
var maximumBeauty = function (items, queries) {
2
items.sort((a, b) => a[0] - b[0]);
3
const n = items.length;
4

5
let mx = items[0][1];
6

7
for (let i = 0; i < n; i++) {
8
mx = Math.max(mx, items[i][1]);
9
items[i][1] = mx;
10
}
11

12
const ans = [];
13

14
for (const q of queries) {
15
let l = 0,
16
r = n - 1,
17
a = 0;
18
while (l <= r) {
19
let mid = Math.floor(l + (r - l) / 2);
20
if (items[mid][0] <= q) {
21
a = items[mid][1];
22
l = mid + 1;
23
} else r = mid - 1;
24
}
25
ans.push(a);
26
}
27

28
return ans;
29
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0