To find a greedy solution for a problem, we have to find a the intuition behind it. This problem looks similar to the problem of activity-selection problem in the text of CLRS (section 16.1
). In that problem, we are to find a set with the maximum size in which every activities are mutually compatible. But this problem has another goal which wants us to find a set maximizing the coverage or resource usage.
The solution to this problem, in my opinion, is to first sort all activities based on their starting time. The intuition behind it is that we want to use the time/value/resources as fast as possible and not waste anything. Then, we start picking the longest activities and check if it is compatible with other activities that have been selected so far. And you continue picking up to the end. If you apply this to the example in the book, it gives you a set with activities {3, 7, 11}
.
It might not be correct for all sets of activities. For example, a set of two activities: activity(1) = <0, 2>
and activity(2) = <1, 5>
.
As you can see, the idea doesn't work in this case. So you have to apply it again but from right to left. (Sort them based on their finishing times and pick those first which are finished first and lasted longer!) At the end you will pick the set with the most coverage!
I might not yet yield the best results. If we add another activity activity(3)=<4, 6>
, those approaches are not the answers. So sorting activities based on their length in a descending order might be the answer.
At the end, one of these three approaches should give the answer.