Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/2250.html
2250. Count Number of Rectangles Containing Each Point
 Difficulty: Medium.
 Related Topics: Array, Binary Search, Binary Indexed Tree, Sorting.
 Similar Questions: Queries on Number of Points Inside a Circle.
Problem
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 bottomleft corner point at the coordinates (0, 0)
and its topright corner point at (li, hi)
.
Return** an integer array count
of length points.length
where count[j]
is the number of rectangles that contain the jth
point.**
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.
Example 1:
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) is 2.
The number of rectangles that contain the point (1, 4) is 1.
Therefore, we return [2, 1].
Example 2:
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) is 1.
The number of rectangles that contain the point (1, 1) is 3.
Therefore, we return [1, 3].
Constraints:

1 <= rectangles.length, points.length <= 5 * 104

rectangles[i].length == points[j].length == 2

1 <= li, xj <= 109

1 <= hi, yj <= 100

All the
rectangles
are unique. 
All the
points
are unique.
Solution (Java, C++, Python)

class Solution { public int[] countRectangles(int[][] rectangles, int[][] points) { int n = rectangles.length; int q = points.length; int[][] es = new int[n + q][]; System.arraycopy(rectangles, 0, es, 0, n); for (int i = 0; i < q; i++) { es[n + i] = new int[] {points[i][0], points[i][1], i}; } Arrays.sort(es, (x, y) > x[0] != y[0] ? (x[0]  y[0]) : x.length  y.length); int[] ct = new int[101]; int[] ans = new int[q]; for (int[] e : es) { if (e.length == 2) { for (int i = 0; i <= e[1]; i++) { ct[i]++; } } else { ans[e[2]] = ct[e[1]]; } } return ans; } }

Todo

class Solution: def countRectangles( self, rectangles: List[List[int]], points: List[List[int]] ) > List[int]: d = defaultdict(list) for x, y in rectangles: d[y].append(x) for y in d.keys(): d[y].sort() ans = [] for x, y in points: cnt = 0 for h in range(y, 101): xs = d[h] cnt += len(xs)  bisect_left(xs, x) ans.append(cnt) return ans ############ # 2250. Count Number of Rectangles Containing Each Point # https://leetcode.com/problems/countnumberofrectanglescontainingeachpoint/ class Solution: def countRectangles(self, rectangles: List[List[int]], points: List[List[int]]) > List[int]: n = len(rectangles) res = [] mp = defaultdict(list) for x, y in rectangles: mp[y].append(x) for k, v in mp.items(): v.sort() for x, y in points: count = 0 for z in range(y, 101): l = mp[z] xIndex = bisect.bisect_left(l, x) count += len(l)  xIndex res.append(count) return res
Explain:
nope.
Complexity:
 Time complexity : O(n).
 Space complexity : O(n).