Minimum Rectangles to Cover Points - Problem

Imagine you're a city planner tasked with covering specific locations with rectangular zones. You have a set of 2D points representing important locations, and you need to cover all of them using the minimum number of rectangles.

Given a 2D integer array points where points[i] = [x_i, y_i] and an integer w, your task is to cover all points with rectangles that:

  • Have their lower edge at y = 0 (ground level)
  • Extend from (x1, 0) to (x2, y2) where x1 ≤ x2 and y2 ≥ 0
  • Have a maximum width constraint: x2 - x1 ≤ w

A point is covered if it lies within or on the boundary of a rectangle. Points can be covered by multiple rectangles. Return the minimum number of rectangles needed to cover all points.

Example: If you have points [(0,1), (1,1), (5,1)] and w=2, you need at least 2 rectangles: one covering (0,1) and (1,1), and another covering (5,1).

Input & Output

example_1.py — Basic Case
$ Input: points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1
Output: 2
💡 Note: After sorting by x-coordinate: [(1,0),(1,4),(1,8),(2,1),(3,5),(4,6)]. First rectangle covers points at x=1 and x=2 (width=1). Second rectangle covers points at x=3 and x=4 (width=1).
example_2.py — Wide Coverage
$ Input: points = [[0,0],[1,1],[2,2]], w = 2
Output: 1
💡 Note: All points have x-coordinates from 0 to 2, which fits within a single rectangle of width 2. One rectangle from (0,0) to (2,∞) covers all points.
example_3.py — Scattered Points
$ Input: points = [[0,0],[5,0],[10,0]], w = 3
Output: 2
💡 Note: Points are at x=0, x=5, x=10. First rectangle covers x=0 with width 3 (covers x=0 to x=3). Second rectangle covers x=5 with width 3 (covers x=5 to x=8), but x=10 needs a third rectangle... Actually, we need one rectangle covering x=0 and another covering x=5 and x=10 - but x=10-x=5 = 5 > 3, so we need separate rectangles for x=5 and x=10. Total: 3 rectangles.

Visualization

Tap to expand
Minimum Rectangles Coverage VisualizationX-Coordinate Axisx=1x=2x=3x=4x=10x=15Rectangle 1 (w=3)Rectangle 2 (w=3)Rectangle 3 (w=3)Covers x=1 to x=4Covers x=10 to x=13Covers x=15 to x=18Algorithm Steps:1. Sort points by x-coordinate: [1,2,3,4,10,15]2. Place rectangle at x=1, extend to cover x=1,2,3,4 (width=3)3. Next uncovered point is x=10, place rectangle there4. Finally, place rectangle at x=15
Understanding the Visualization
1
Sort Locations
Arrange all points by x-coordinate to process them systematically from left to right
2
Greedy Placement
For each uncovered point, place a rectangle and extend it as far right as the width constraint allows
3
Maximize Coverage
Each rectangle covers all points within its width, minimizing the total number needed
4
Optimal Solution
This greedy approach guarantees the minimum number of rectangles
Key Takeaway
🎯 Key Insight: After sorting by x-coordinate, we can greedily place each rectangle to start at the leftmost uncovered point and extend as far right as possible within the width constraint. This guarantees the minimum number of rectangles.

Time & Space Complexity

Time Complexity
⏱️
O(2^n * n)

We try all possible subsets of points (2^n) and for each subset check validity (O(n))

n
2n
Quadratic Growth
Space Complexity
O(n)

Recursion stack depth and temporary storage for combinations

n
2n
Linearithmic Space

Constraints

  • 1 ≤ points.length ≤ 105
  • points[i].length == 2
  • 0 ≤ xi, yi ≤ 109
  • 0 ≤ w ≤ 109
  • Note: Only x-coordinates matter for rectangle placement
Asked in
Google 35 Microsoft 28 Amazon 22 Meta 18
24.7K 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