The following is a practice interview question that was given to me by someone, and I\'m not sure what the best solution to this is:
Given a set of ranges
How about using an interval tree for queries? (https://en.m.wikipedia.org/wiki/Interval_tree) I'm not sure if greedy could work here or not. If we look at the last set of choices, overlapping with the high point in R
, there's a possibility of overlap between the earlier choices for each one of those, for example:
R = (2,10) and we have (8,10) and (7,10) both overlapping with (6,8)
In that case, we only need to store one value for (6,8)
as a second leg of the path; and visiting (6,8)
again as we make longer paths towards the low point in R
would be superfluous since we already know (6,8)
was visited with a lower leg count. So your idea of eliminating intervals as we go makes sense. Could something like this work?
leg = 1
start with the possible end (or beginning) intervals
label these intervals with leg
until end of path is reached:
remove the intervals labeled leg from the tree
for each of those intervals labeled leg:
list overlapping intervals in the chosen direction
leg = leg + 1
label the listed overlapping intervals with leg