Count Number of Rectangles Containing Each Point LeetCode solution in JavaScript
1var countRectangles = function (rectangles, points) {2let buckets = Array(101)3.fill(0)4.map(() => []);5for (let [x, y] of rectangles) {6buckets[y].push(x);7}8for (let i = 0; i < 101; i++) buckets[i].sort((a, b) => a - b);910let res = [];11for (let point of points) {12let sum = 0;13for (let j = point[1]; j < 101; j++) {14// lowest index >= point[0]15let index = lower_bound(buckets[j], point[0]);16sum += buckets[j].length - index;17}18res.push(sum);19}20return res;2122function lower_bound(arr, targ) {23let low = 0,24high = arr.length;25while (low < high) {26let mid = Math.floor((low + high) / 2);27if (arr[mid] >= targ) high = mid;28else low = mid + 1;29}30return low;31}32};