Given a list of time ranges, I need to find the maximum number of overlaps.
Following is a dataset showing a 10 minute interval of calls, from which I am trying to find the maximum number of active lines in that interval. ie. from the example below, what is the maximum number of calls that were active at the same time:
CallStart CallEnd
2:22:22 PM 2:22:33 PM
2:22:35 PM 2:22:42 PM
2:22:36 PM 2:22:43 PM
2:22:46 PM 2:22:54 PM
2:22:49 PM 2:27:21 PM
2:22:57 PM 2:23:03 PM
2:23:29 PM 2:23:40 PM
2:24:08 PM 2:24:14 PM
2:27:37 PM 2:39:14 PM
2:27:47 PM 2:27:55 PM
2:29:04 PM 2:29:26 PM
2:29:31 PM 2:29:43 PM
2:29:45 PM 2:30:10 PM
If anyone knows an alogrithm or can point me in the right direction, I would be grateful.
TIA,
Steve F
Here is a working algorithm in Python
Following must work:
numberOfCalls
to 0 (count variable)Run through your time values and:
Complexity: O(n log(n)) for sorting, O(n) to run through all records
I think an important element of good solution for this problem is to recognize that each end time is >= the call's start time and that the start times are ordered. So rather than thinking in terms of reading the whole list and sorting we only need to read in order of start time and merge from a min-heap of the end times. This also addresses the comment Sanjeev made about how ends should be processed before starts when they have the exact same time value by polling from the end time min-heap and choosing it when it's value is <= the next start time.