#2250

Count Number of Rectangles Containing Each Point

expert · 1060 · lc medium +32 · verified · 37.4% accepted · 554 likes · top 16%

Description

You are given a 2D integer array rectangles where rectangles[i] = [li, hi] describes a rectangle with width li and height hi whose bottom-left corner is at the origin. You are also given a 2D integer array points where points[j] = [xj, yj].

Return an integer array count where count[j] is the number of rectangles that contain point j.

Rectangle i contains point j when 0 <= xj <= li and 0 <= yj <= hi. Points on the boundary count as contained.

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].

Code

1
2
3