I need to find an algorithm that solves the following problem:
Given a list of intervals (leftBound,RightBound) which is the most efficient algorithm to group the intervals in this behavior:
Intervals: (1,4),(6,9),(1,3),(4,8),(6,9),(2,7),(10,15)
Wanted Solution:
Group (2,3) contains (1,3), (1,4), (2,7)
Group (6,8) contains (4,8), (6,9)
Group (10,15) contains (10,15)
Of course there are different possible solutions: (2,7) could also be in the second group instead.
My approach is to sort the intervals ascending by their left bound and if they have the same left bound descending by their right bound. Then i just loop over the sorted intervals and try to add them to the group I just build before. If this is not possible i build a new group for this interval and continue looping over the remaining orders.
Does this algorithm gurantee, that i receive the lowest possible numbers of different groups for my intervals? Could you say that this is a greedy approach for solving this problem?