Count Number of Rectangles Containing Each Point LeetCode solution in JavaScript

1
var countRectangles = function (rectangles, points) {
2
let buckets = Array(101)
3
.fill(0)
4
.map(() => []);
5
for (let [x, y] of rectangles) {
6
buckets[y].push(x);
7
}
8
for (let i = 0; i < 101; i++) buckets[i].sort((a, b) => a - b);
9

10
let res = [];
11
for (let point of points) {
12
let sum = 0;
13
for (let j = point[1]; j < 101; j++) {
14
// lowest index >= point[0]
15
let index = lower_bound(buckets[j], point[0]);
16
sum += buckets[j].length - index;
17
}
18
res.push(sum);
19
}
20
return res;
21

22
function lower_bound(arr, targ) {
23
let low = 0,
24
high = arr.length;
25
while (low < high) {
26
let mid = Math.floor((low + high) / 2);
27
if (arr[mid] >= targ) high = mid;
28
else low = mid + 1;
29
}
30
return low;
31
}
32
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0