Finding (number of) overlaps in a list of time ranges

后端 未结 9 628
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 19:53

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 f

相关标签:
9条回答
  • 2020-12-04 20:27

    In my opinion greedy algorithm will do the needful. The problem is similar to find out the number of platforms required for given trains timetable. So the number of overlaps will be the number of platforms required.
    callStart times are sorted. Start putting each call in an array(a platform). So for call i and (i + 1), if callEnd[i] > callStart[i+1] then they can not go in the same array (or platform) put as many calls in the first array as possible. Then repeat the process with rest ones till all calls are exhausted. In the end, number of arrays are maximum number of overlaps. And the complexity will be O(n).

    0 讨论(0)
  • 2020-12-04 20:29

    The following page has examples of solving this problem in many languages: http://rosettacode.org/wiki/Max_Licenses_In_Use

    0 讨论(0)
  • 2020-12-04 20:32

    Following must work:

    1. Sort all your time values and save Start or End state for each time value.
    2. Set numberOfCalls to 0 (count variable)
    3. Run through your time values and:

      • increment numberOfCalls if time value marked as Start
      • decrement numberOfCalls if time value marked as End
      • keep track of maximum value of numberOfCalls during the process (and time values when it occurs)

    Complexity: O(n log(n)) for sorting, O(n) to run through all records

    0 讨论(0)
提交回复
热议问题