183k views
5 votes
Consider the interval cover from before, but with weights assigned to each interval. You are given a collection C of n intervals in the real line, each with an associated positive weight. Your goal is to find a set S of intervals from C so that no two elements of S overlap and so that subject to this, the sum of the weights of these intervals is as large as possible. Give an O(nlog(n)) time algorithm for this problem.

User Memoizer
by
8.2k points

1 Answer

7 votes

Final answer:

To solve this problem, we can use a greedy algorithm to find the set of intervals with the largest sum of weights and no overlapping intervals. The algorithm has a time complexity of O(nlog(n)).

Step-by-step explanation:

To solve this problem, we can use a greedy algorithm. The idea is to sort the intervals in non-decreasing order of their end points. Then, we can start with an empty set S and iterate through the sorted intervals. For each interval, if it does not overlap with any interval in S, we can add it to S. This ensures that no two intervals in S will overlap. We continue this process until we have processed all intervals.

To implement this algorithm, we can use a min-heap to keep track of the intervals sorted by their end points. Initially, we can insert all the intervals into the min-heap. Then, we repeatedly extract the interval with the smallest end point from the heap, check if it overlaps with any interval in S, and add it to S if it does not. This process can be done in O(nlog(n)) time, where n is the number of intervals.

User Muhammadjon
by
8.9k points