Count Number of Rectangles Containing Each Point - Problem

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. Note that points that lie on the edges of a rectangle are also considered to be contained by that rectangle.

Input & Output

Example 1 — Basic Rectangle Coverage
$ Input: rectangles = [[3,1],[1,1],[1,2]], points = [[1,1],[1,0]]
Output: [3,3]
💡 Note: Point (1,1) is contained in all 3 rectangles: [3,1] (1≤3, 1≤1), [1,1] (1≤1, 1≤1), [1,2] (1≤1, 1≤2). Point (1,0) is also contained in all 3 rectangles: [3,1] (1≤3, 0≤1), [1,1] (1≤1, 0≤1), [1,2] (1≤1, 0≤2).
Example 2 — Edge and Corner Cases
$ Input: rectangles = [[1,1],[2,2],[3,3]], points = [[1,1],[2,2],[0,0]]
Output: [1,2,3]
💡 Note: Point (1,1) fits in [1,1], (2,2) fits in [2,2] and [3,3], (0,0) fits in all rectangles as it's at the origin.
Example 3 — Outside All Rectangles
$ Input: rectangles = [[1,1],[2,2]], points = [[3,3],[4,4]]
Output: [0,0]
💡 Note: Both points (3,3) and (4,4) are outside all rectangles, so count is 0 for each.

Constraints

  • 1 ≤ rectangles.length ≤ 5 × 104
  • rectangles[i].length == 2
  • 1 ≤ li, hi ≤ 109
  • 1 ≤ points.length ≤ 5 × 104
  • points[j].length == 2
  • 0 ≤ xj, yj ≤ 109

Visualization

Tap to expand
Count Rectangles Containing Points INPUT Rectangles at origin (0,0) 0 1 2 3 [3,1] [1,2] P1(1,1) P2(1,0) rectangles: [3,1] [1,1] [1,2] points: [1,1] [1,0] Check: x <= l and y <= h for each point-rectangle pair ALGORITHM STEPS 1 Group by Height Group rectangles by their h value h=1: [3,1] h=2: [1,1],[1,2] 2 Sort Lengths Sort lengths in each height group h=1: [1,3] sorted h=2: [1] sorted 3 Binary Search For each point, search heights >= y P1(1,1): Check h>=1 groups Binary search: lengths >= 1 Count valid rectangles 4 Accumulate Count Sum counts from all valid groups P1: h=1(2) + h=2(1) = 3 P2: h=1(2) + h=2(0) = 2 (y=0 fits all, y=1 height check) FINAL RESULT Rectangle count per point: Point [1,1] 3 Point [1,0] 2 Output Array: 3 2 [3, 2] OK - Verified! Key Insight: Coordinate Compression Since height values are limited (max 100), group rectangles by height and store sorted lengths. For each point, iterate heights >= point.y and use binary search to count rectangles with length >= point.x. Time: O(n*log(n) + m*100*log(n)) where n=rectangles, m=points. Space: O(n) for grouped storage. TutorialsPoint - Count Number of Rectangles Containing Each Point | Coordinate Compression Approach
Asked in
Google 25 Amazon 18 Microsoft 12 Facebook 10
23.4K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen