I have set of overlapping intervals, i have to choose one element from the respective interval such that when they are grouped there are minimum gaps in the selection.
By Grouping I mean consecutive elements are grouped. And if there are no consecutive elements from other intervals for an element then this is considered as group with one element
By minimize gaps I mean, we have reduce number of such groups and try to form the larger ones
I saw about interval trees and thought that might help but not sure how to use that for my benefit
Please tell me what approach should I take to solve the problem.
Example:
Intervals (inclusive of boundaries)
[1,2]
[2,4]
[3,7]
[6,11]
[9,11]
[5,11]
[10,14]
[13,14]
Possible Solution
[1,2] ==> 2
[2,4] ==> 3
[3,7] ==> 4
[6,11] ==> 10
[9,11] ==> 9
[5,11] ==> 11
[10,14] ==> 12
[13,14] ==> 13
Groups formed by choosing above elements
2,3,4 and 9,10,11,12,13
So there is only one gap 4 to 9