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,2]
💡 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 contained in 2 rectangles: [3,1] (1≤3, 0≤1), [1,2] (1≤1, 0≤2), but not [1,1] since 0≤1 fails.
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
Rectangle Point Containment ProblemRect1: [3,1]Rect2: [1,1]Rect3: [1,2]Point(1,1)Point(1,0)Containment CheckPoint (1,1): 0≤1≤3 ✓, 0≤1≤1 ✓ → In Rect1Point (1,1): 0≤1≤1 ✓, 0≤1≤1 ✓ → In Rect2Point (1,1): 0≤1≤1 ✓, 0≤1≤2 ✓ → In Rect3Count rectangles containing each pointOutput: [3, 2] - Point(1,1) in 3 rects, Point(1,0) in 2 rects
Understanding the Visualization
1
Input
Rectangles with dimensions and points to check
2
Check Containment
Determine which rectangles contain each point
3
Count Results
Return count array for each point
Key Takeaway
🎯 Key Insight: Since all rectangles are anchored at origin (0,0), containment is simply checking if point coordinates are within rectangle dimensions
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