Problem Description
Given a set of distinct points on a 2D plane and an integer w, cover all the points using the minimum number of rectangles. Each rectangle has its lower side on the x-axis (y = 0) with a left end at (x1, 0) and an upper end at (x2, y2) such that x1 <= x2, y2 >= 0, and the horizontal width constraint x2 - x1 <= w holds. A point is covered if it lies within or on the boundary of some rectangle. The objective is to determine the minimum number of such rectangles required to cover all the given points.
Key Insights
- The vertical coverage of a rectangle can always be extended to cover the highest point within its horizontal interval, so the y-coordinates do not affect the placement horizontally.
- The problem essentially reduces to covering points (based on their x-coordinates) with intervals of length w.
- A greedy algorithm works by sorting the points by their x-coordinate and placing a rectangle starting at the leftmost uncovered point, covering as many points as possible within x + w.
Space and Time Complexity
Time Complexity: O(n log n) due to sorting the points. Space Complexity: O(n) for storing the sorted points (or O(1) additional space if sorting in-place).
Solution
Sort the points by their x-coordinate. Start with the leftmost point and place a rectangle that covers all points with x-coordinates in the interval [start, start+w]. Skip all points covered by this rectangle and then choose the next uncovered point to place another rectangle. Continue until all points are covered. This greedy strategy guarantees the minimal number of rectangles since each rectangle is maximally extended to include as many points as possible within the allowed width.