Finding minimum number of points which covers entire set of intervals?

后端 未结 1 801
时光说笑
时光说笑 2021-01-05 00:50

Given a set of intervals [x,y] where 0 <= x,y <= 2000 how to find minimum number of points which can cover(i.e. Every interval should contain at least o

相关标签:
1条回答
  • 2021-01-05 01:12

    You can use a greedy algorithm:

    1. Sort all intervals by their end points(in increasing order).

    2. Iterate over a sorted array of intervals. When an interval is over, there are two options:

      1. It is already covered by some point. Nothing should be done in this case.
      2. It is not covered yet. Then the end point of this interval should be inserted into to the resulting set.

    The resulting set generated by this algorithm is optimal.

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