So I stumbled across this problem someone send in a Discord gc (irrelevant), and it seemed really intriguing. We have a certain target interval (in this example, [20, 100]) and then some other secondary intervals (in this example [20, 45], [30, 75], [40, 80], [60, 95] and [90, 100]). Each secondary interval has a cost, as shown in the picture (the number next to the line is the cost). Our goal is to cover the target interval with the CHEAPEST possible set of secondary intervals
For example, the interval [20, 100] can be covered at minimum cost by choosing the intervals with costs 10 + 20 + 5 + 15 = 50. (Choosing the interval with cost 30 instead of the interval with cost 20 would also achieve the desired result but at a higher total cost.)
What would be the best approach to this?
The first thing I tried was developing a greedy approach algorithm in c++, like shown to other similar problems online But I found trouble in the "cheapest" part. How can I make sure I'm using the cheapest interval/subset of intervals that substitutes the most expensive?
EDIT: the problem actually only requires to print the cost of the intervals needed to cover the target interval, not the intervals themselves