Minimum Rectangles to Cover Points Problem
Description
LeetCode Problem 3111.
You are given a 2D integer array points, where points[i] = [x_i, y_i]. You are also given an integer w. Your task is to cover all the given points with rectangles.
Each rectangle has its lower end at some point (x_1, 0) and its upper end at some point (x_2, y_2), where x_1 <= x_2, y_2 >= 0, and the condition x_2 - x_1 <= w must be satisfied for each rectangle.
A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.
Return an integer denoting the minimum number of rectangles needed so that each point is covered by at least one rectangle*.*
Note: A point may be covered by more than one rectangle.
Example 1:

Input: points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1
Output: 2
Explanation:
The image above shows one possible placement of rectangles to cover the points:
- A rectangle with a lower end at (1, 0) and its upper end at (2, 8)
- A rectangle with a lower end at (3, 0) and its upper end at (4, 8)
Example 2:

Input: points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2
Output: 3
Explanation:
The image above shows one possible placement of rectangles to cover the points:
- A rectangle with a lower end at (0, 0) and its upper end at (2, 2)
- A rectangle with a lower end at (3, 0) and its upper end at (5, 5)
- A rectangle with a lower end at (6, 0) and its upper end at (6, 6)
Example 3:

Input: points = [[2,3],[1,2]], w = 0
Output: 2
Explanation:
The image above shows one possible placement of rectangles to cover the points:
- A rectangle with a lower end at (1, 0) and its upper end at (1, 2)
- A rectangle with a lower end at (2, 0) and its upper end at (2, 3)
Constraints:
1 <= points.length <= 10^5points[i].length == 20 <= x_i == points[i][0] <= 10^90 <= y_i == points[i][1] <= 10^90 <= w <= 10^9- All pairs
(x_i, y_i)are distinct.
Solution
Approach: Greedy + Sorting
Rectangle height is irrelevant (it is unbounded in effect), so only the x-coordinates matter. Sort the points by x and keep x1, the rightmost x the last rectangle covers (start with -1).
Scan the points. If a point's x is greater than x1, the current rectangle cannot reach it, so open a new one starting at this point: count it and set x1 = x + w. The count at the end is the minimum.
Complexity: O(n log n) time, O(log n) space, where n is the number of points.
class Solution:
def minRectanglesToCoverPoints(self, points: List[List[int]], w: int) -> int:
points.sort()
ans, x1 = 0, -1
for x, _ in points:
if x > x1:
ans += 1
x1 = x + w
return ans