Count Number of Rectangles Containing Each Point - Problem
Count Rectangles Containing Each Point
Imagine you have a collection of rectangles, all positioned with their bottom-left corner at the origin (0, 0) and extending to different top-right corners. You also have several query points scattered across the coordinate plane.
Your task is to determine how many rectangles contain each query point. A rectangle defined by
Key Points:
• Points on the edges and corners of rectangles are considered inside
• All rectangles share the same bottom-left corner at origin
• Return an array where each element represents the count for the corresponding query point
Imagine you have a collection of rectangles, all positioned with their bottom-left corner at the origin (0, 0) and extending to different top-right corners. You also have several query points scattered across the coordinate plane.
Your task is to determine how many rectangles contain each query point. A rectangle defined by
[length, height] extends from (0, 0) to (length, height). A point (x, y) is considered inside a rectangle if 0 ≤ x ≤ length and 0 ≤ y ≤ height.Key Points:
• Points on the edges and corners of rectangles are considered inside
• All rectangles share the same bottom-left corner at origin
• Return an array where each element represents the count for the corresponding query point
Input & Output
example_1.py — Basic Case
$
Input:
rectangles = [[3,4],[1,1],[1,4]], points = [[1,1],[1,2],[3,1]]
›
Output:
[2, 1, 2]
💡 Note:
Point (1,1): Inside rectangles [3,4] and [1,4]. Point (1,2): Only inside rectangle [3,4]. Point (3,1): Inside rectangles [3,4] and [1,4].
example_2.py — Edge Points
$
Input:
rectangles = [[2,2],[3,3]], points = [[2,2],[0,0],[1,3]]
›
Output:
[2, 2, 1]
💡 Note:
Point (2,2): On edge/corner of both rectangles, counts as inside both. Point (0,0): At origin, inside both rectangles. Point (1,3): Only inside the larger rectangle [3,3].
example_3.py — Outside Points
$
Input:
rectangles = [[1,1],[2,2]], points = [[3,3],[0,3],[3,0]]
›
Output:
[0, 0, 0]
💡 Note:
All query points are outside both rectangles. Point (3,3) exceeds both dimensions, (0,3) and (3,0) exceed one dimension each.
Constraints
- 1 ≤ rectangles.length, points.length ≤ 5 × 104
- rectangles[i] = [li, hi] where 1 ≤ li, hi ≤ 109
- points[j] = [xj, yj] where 0 ≤ xj, yj ≤ 109
- All rectangles have bottom-left corner at (0,0)
Visualization
Tap to expand
Understanding the Visualization
1
Setup Rectangles
Position rectangles with bottom-left at origin, extending to different top-right corners
2
Place Query Points
Scatter query points across the coordinate plane
3
Check Containment
For each point, count how many rectangles contain it (including edge cases)
4
Optimize with Sorting
Group rectangles by dimensions and use binary search for efficiency
Key Takeaway
🎯 Key Insight: Since all rectangles share the same bottom-left corner at origin, we can optimize by grouping rectangles by length and using binary search on heights to efficiently count containment without checking every rectangle individually.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code