1101. Rectangles Around Points

Medium3.5 points estimatedLine SweepBinary SearchSorting3 hints

A greenhouse company stocks glass panels that are all laid with one corner on the origin of a floor plan. Panel i is described by rectangles[i] = [w, h] and covers every spot from (0, 0) to (w, h), border included. Several panels may have identical sizes and they are allowed to overlap.

Inspectors will visit the spots in points, where points[j] = [x, y]. A panel shelters a spot when x <= w and y <= h. Return an array result with the same length as points, where result[j] is the number of panels sheltering points[j]. Both the panels and the spots are given in no particular order, and the answers must follow the order of points.

Example 1

Input:
rectangles = [[4,3],[6,2],[2,5]]points = [[2,2],[5,1],[3,4]]
Output:
[3,1,0]
Explanation:

Point (2,2) is sheltered by all three panels, (5,1) only by the 6 by 2 panel, and (3,4) by none.

Example 2

Input:
rectangles = [[3,3],[3,3]], points = [[3,3],[4,1]]
Output:
[2,0]
Explanation:

Identical panels both count, and a spot exactly on the corner (3,3) is still sheltered.

Constraints

1 ≤ rectangles.length, points.length ≤ 5 * 104

rectangles[i] = [w, h] and points[j] = [x, y]

1 ≤ w, h, x, y ≤ 109

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 3,200 msC++ 800 msJava 1,600 msJavaScript 1,600 msTypeScript 1,600 ms

Expected complexity

Time
O((n + q) log n)
Space
O(n + q)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…