Union of intervals

前端 未结 6 1814
花落未央
花落未央 2020-12-01 14:32

I\'ve got a class representing an interval. This class has two properties \"start\" and \"end\" of a comparable type. Now I\'m searching for an efficient algorithm to take t

6条回答
  •  误落风尘
    2020-12-01 15:20

    Use the sweep line algorithm. Basically, you sort all the values in a list (while keeping whether it's beginning or end of the interval along with each item). This operation is O(n log n). Then you loop in a single pass along the sorted items and compute the intervals O(n).

    O(n log n) + O(n) = O(n log n)

提交回复
热议问题