Given a set of closed intervals, find the smallest set of numbers that covers all the intervals. If there are multiple smallest sets, return any of them.
OR
Let X be a set of n intervals on the real line. We say that a set of points P “stabs” X if every interval in X contains at least one point in P. Compute the smallest set of points that stabs X.
You are responsible for collecting signatures from all tenants of a certain building. For each tenant, you know a period of time when he or she is at home. You would like to collect all signatures by visiting the building as few times as possible.
Given a set of n segments [[a0, b0], [a1, b1], . . . , [an-1, bn-1]] with integer coordinates on a line, find the minimum number m of points such that each segment contains at least one point. That is, find a set of integers X of the minimum size such that for any segment [ai, bi] there is a point x ∈ X such that ai ≤ x ≤ bi.
Input: intervals =[[0,3],[2,6],[3,4],[6,9]]Output: [3,6]Explanation:
- The set [3,6] covers all intervals.- Interval [0,3]is covered by 3.- Interval [2,6]is covered by 3 and 6.- Interval [3,4]is covered by 3.- Interval [6,9]is covered by 6.
To cover all intervals with the minimum number of points, we can focus on the end points of the intervals. By sorting intervals by their endpoints, we can efficiently determine the smallest set of points required to cover the intervals. Whenever a point from the sorted intervals is not covered, it can be added to the solution set.