You are given a 2D integer array rectangles where rectangles[i] = [li, hi] indicates that ith rectangle has a length of li and a height of hi. You are also given a 2D integer array points where points[j] = [xj, yj] is a point with coordinates (xj, yj).
The ith rectangle has its bottom-left corner point at the coordinates (0, 0) and its top-right corner point at (li, hi).
Return an integer arraycountof lengthpoints.lengthwherecount[j]_is the number of rectangles thatcontain the _jthpoint.
The ith rectangle contains the jth point if 0 <= xj <= li and 0 <= yj <= hi. Note that points that lie on the edges of a rectangle are also considered to be contained by that rectangle.
Input: rectangles =[[1,2],[2,3],[2,5]], points =[[2,1],[1,4]]Output: [2,1]Explanation:
The first rectangle contains no points.The second rectangle contains only the point(2,1).The third rectangle contains the points(2,1) and (1,4).The number of rectangles that contain the point(2,1)is2.The number of rectangles that contain the point(1,4)is1.Therefore, we return[2,1].
Input: rectangles =[[1,1],[2,2],[3,3]], points =[[1,3],[1,1]]Output: [1,3]Explanation: The first rectangle contains only the point(1,1).The second rectangle contains only the point(1,1).The third rectangle contains the points(1,3) and (1,1).The number of rectangles that contain the point(1,3)is1.The number of rectangles that contain the point(1,1)is3.Therefore, we return[1,3].
For each point, we want to count how many rectangles contain it. If we group rectangles by height and sort their lengths, we can efficiently count, for each point, how many rectangles have both length and height at least as large as the point’s coordinates using binary search.
Group all rectangles by their height in a map from height to a list of lengths.
For each height, sort the list of lengths.
For each point, for all heights greater than or equal to the point’s y-coordinate, use binary search to count how many rectangles have length at least as large as the point’s x-coordinate.
Sum these counts for all heights to get the answer for each point.
classSolution {
publicint[]countRectangles(int[][] rectangles, int[][] points) {
TreeMap<Integer, List<Integer>> h2l =new TreeMap<>();
for (int[] r : rectangles) h2l.computeIfAbsent(r[1], x ->new ArrayList<>()).add(r[0]);
for (List<Integer> v : h2l.values()) Collections.sort(v);
int[] ans =newint[points.length];
for (int i = 0; i < points.length; i++) {
int cnt = 0;
for (int h : h2l.tailMap(points[i][1]).keySet()) {
List<Integer> v = h2l.get(h);
int idx = Collections.binarySearch(v, points[i][0]);
if (idx < 0) idx =-idx - 1;
cnt += v.size() - idx;
}
ans[i]= cnt;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
funcountRectangles(rectangles: Array<IntArray>, points: Array<IntArray>): IntArray {
val h2l = sortedMapOf<Int, MutableList<Int>>()
for (r in rectangles) h2l.getOrPut(r[1]) { mutableListOf() }.add(r[0])
for (v in h2l.values) v.sort()
val ans = IntArray(points.size)
for ((i, p) in points.withIndex()) {
var cnt = 0for ((h, v) in h2l.tailMap(p[1])) {
val idx = v.binarySearch(p[0]).let { if (it < 0) -it - 1elseit }
cnt += v.size - idx
}
ans[i] = cnt
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution:
defcountRectangles(self, rectangles: list[list[int]], points: list[list[int]]) -> list[int]:
from collections import defaultdict
import bisect
h2l = defaultdict(list)
for l, h in rectangles:
h2l[h].append(l)
for v in h2l.values():
v.sort()
ans = []
for x, y in points:
cnt =0for h in h2l:
if h >= y:
v = h2l[h]
idx = bisect.bisect_left(v, x)
cnt += len(v) - idx
ans.append(cnt)
return ans
use std::collections::BTreeMap;
impl Solution {
pubfncount_rectangles(rectangles: Vec<Vec<i32>>, points: Vec<Vec<i32>>) -> Vec<i32> {
letmut h2l = BTreeMap::new();
for r in rectangles {
h2l.entry(r[1]).or_insert(vec![]).push(r[0]);
}
for v in h2l.values_mut() {
v.sort();
}
letmut ans =vec![];
for p in points {
letmut cnt =0;
for (&h, v) in h2l.range(p[1]..=i32::MAX) {
let idx = v.binary_search(&p[0]).unwrap_or_else(|x| x);
cnt += v.len() - idx;
}
ans.push(cnt asi32);
}
ans
}
}
⏰ Time complexity: O((n + m) * log n), where n is the number of rectangles and m is the number of points. For each point, we may check up to 100 heights, and for each, binary search in a sorted list of lengths.
🧺 Space complexity: O(n), for storing the grouped rectangles.