How to identify the maximum number of overlapping date ranges?

前端 未结 1 1772
后悔当初
后悔当初 2021-01-26 19:40

This question might be similar to:

  • Determine Whether Two Date Ranges Overlap
  • Multiple Date range comparison for overlap: how to do it efficiently?
相关标签:
1条回答
  • 2021-01-26 20:00
    • For each range, create two Tuple<DateTime, int>s with values start, +1 and end, -1
    • Sort the collection of tuples by date
    • Iterate through the sorted list, adding the number part of the tuple to a running total, and keeping track of the maximum value reached by the running total
    • Return the maximum value the running total reaches

    Executes in O(n log n) because of the sort. There's probably a more efficient way.

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