【问题标题】:Finding longest overlapping interval pair寻找最长的重叠区间对
【发布时间】:2018-04-12 02:20:47
【问题描述】:

假设我有一个 n 个整数区间 [a,b] 的列表,每个表示集合 S = {a, a+1, ...b}。重叠定义为|S_1 \cap S_2|。示例:[3,6] 和 [5,9] 在 [5,6] 上重叠,因此其长度为 2。任务是在 Little-O(n^2) 中找到两个重叠最长的区间,仅使用递归和动态规划。

朴素的方法显然是蛮力的,它不符合时间复杂度条件。我也没有成功尝试扫描线算法和/或最长公共子序列算法。

我只是找不到将其划分为子问题的方法。任何想法将不胜感激。

还发现了这个,我认为这根本不起作用:

Finding “maximum” overlapping interval pair in O(nlog(n))

【问题讨论】:

  • 如果问题是关于寻找算法而不是实现它,也许Computer Science 更适合? (但请记住don't crosspost 并在询问之前阅读他们的帮助中心)
  • 只是说“在我看来根本不起作用”并不能证明它实际上不起作用。您需要解释为什么它不起作用以及为什么您的问题不是重复的。
  • 蛮力会在 O(n^2) 中运行,所以它应该满足问题要求
  • @Mitchel0022 请查看 Little-O 和 Big-O 表示法之间的区别

标签: algorithm intervals overlap schedule


【解决方案1】:

这是一种需要N log(N) 时间的方法。

将每个间隔[a,b] [c,d] 分解成这样的一对数组:

pair<a,-1>
pair<b,a>
pair<c,-1>
pair<d,c>

sort these pairs in increasing order. Since interval starts are marked as -1, in case of ties interval they should come ahead of interval ends.

for i = 0 to end of the pair array
    if current pair represents interval start
        put it in a multiset
    else
        remove the interval start corresponding to this interval end from the multiset.
        if the multiset is not empty
            update the maxOverlap with (current_interval_end - max(minimum_value_in_multiset,start_value_of_current_interval)+1)

此方法应将maxOverlap 更新为可能的最高值。

【讨论】:

    【解决方案2】:

    保留有关两个最大重叠间隔 max1max2 的信息(开头为空)。

    按值x 对输入列表[x1, y1] .. [xn, yn] = I1..In 进行排序,如果遇到相等则丢弃两个区间中较短的一个。在抛出间隔时,请保持 max1max2 更新。

    对于每个区间,添加一个线性时间属性max,显示所有先前区间中最大的y值(在排序列表中):

    rollmax = −∞
    for j = 1..n do
      Ij.max = rollmax
      rollmax = max(rollmax, Ij.y)
    

    在排序、过滤和扩展的输入列表上执行以下查询。它使用一个不断扩展的间隔子列表,该子列表比当前搜索的间隔 Ii 更小,作为递归函数 SearchOverlap 的输入。

    for i = 2..n do
      SearchOverlap(Ii, 1, i − 1)
    return {max1, max2}
    

    函数SearchOverlap 使用divide and conquer 方法遍历排序列表Il, .. Ir。它将这样的列表想象成一棵完整的二叉树,以区间Ic 作为其本地根。测试Ic.max &lt; I.max 用于始终决定在与I 重叠最大的区间方向上遍历二叉树(向左/向右)。请注意,I 是查询的区间,与 log(n) 其他区间进行比较。另请注意,在此类遍历中可能会传递最大可能的重叠间隔,因此在函数 SearchOverlap 的开头检查最大重叠。

    SearchOverlap(I , l, r)
    c = ceil(Avg(l, r)) // Central element of queried list
    if Overlap(max1, max2) < Overlap(I , Ic) then
      max1 = I
      max2 = Ic
    if l ≥ r then
      return
    if Ic.max < I.max then
      SearchOverlap(I , c + 1, r)
    else
      SearchOverlap(I , l, c − 1)
    return
    

    最后返回最大的重叠间隔(如果不为空)。总复杂度为O(n log(n))

    【讨论】:

      猜你喜欢
      • 2018-07-06
      • 2011-06-03
      • 2019-11-05
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 2022-08-21
      • 1970-01-01
      • 2013-08-17
      相关资源
      最近更新 更多