Count Number of Rectangles Containing Each Point

You are given a 2D integer array rectangles where rectangles[i] = [li, hi] indicates that the i^th 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 i^th rectangle has its bottom-left corner point at the coordinates (0, 0) and its top-right 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 j^th point.

The i^th rectangle contains the j^th point if 0 <= xj <= li and 0 <= yj <= hi. Points that lie on the edges of a rectangle are also considered to be contained by that rectangle.

Example 1
Inputrectangles = [[1,2],[2,3],[2,5]], points = [[2,1],[1,4]]
Output[2,1]
The point (2, 1) is contained by 2 rectangles, and the point (1, 4) is contained by 1 rectangle.
Example 2
Inputrectangles = [[1,1],[2,2],[3,3]], points = [[1,3],[1,1]]
Output[1,3]
The point (1, 3) is contained by 1 rectangle, and the point (1, 1) is contained by 3 rectangles.

Constraints

  • 1 <= rectangles.length, points.length <= 5 * 10^4
  • rectangles[i].length == points[j].length == 2
  • 1 <= li, xj <= 10^9
  • 1 <= hi, yj <= 100
  • All the rectangles are unique.
  • All the points are unique.

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate