Range intersection is a simple, but non-trivial problem.
Its has been answered twice already:
- http://stackoverflow.com/questions/224878/find-number-range-intersection
- http://stackoverflow.com/questions/143552/comparing-date-ranges
The first solutions is O(n) and the second solution is for a database (which is less than O(n) of course).
I have the same problem, but for a large n and I am not within a database.
This problem seems to be very similar to http://stackoverflow.com/questions/303243/store-2d-points-for-quick-retrieval-of-those-inside-a-rectangle but I don't see how it maps.
So what data structure would you store the set of ranges in, such that a search on a range costs less than O(n)? (Extra credit for using libraries available for Java)
EDIT:
I want to get a subset of all intersecting ranges, meaning the search range could intersect multiple ranges.
The method that needs to be less than O(n) in Java is:
public class RangeSet {
....
public Set<Range> intersects(Range range);
....
}
Where Range is just a class containing a pair of int start and end.
This is not an impossible question, I already have the solution, I just wanted to see if there was a more standard/simpler way of doing it