【发布时间】:2025-12-17 06:35:01
【问题描述】:
如何有效地将一组区间(input 集)拆分为一组最小的不相交区间(output 集),这样所有的输入集的区间可以表示为输出集的区间的并集?
例子:
Input: [0,9] [2,12]
Output: [0,1] [2,9] [10,12]
Test :
[0,9] = [0,1] ∪ [2,9]
[2,12] = [2,9] ∪ [10,12]
Input: [0,Infinity] [1,5] [4,6]
Output: [0,0] [1,3] [4,5] [6,6] [7,Infinity]
Test :
[0,Infinity] = [0,0] ∪ [1,3] ∪ [4,5] ∪ [6,6] ∪ [7,Infinity]
[1,5] = [1,3] ∪ [4,5]
[4,6] = [4,5] ∪ [6,6]
我需要在 Javascript 中执行此操作。这是我尝试过的想法:
// The input is an array of intervals, like [[0,9], [2,12]], same for the output
// This function converts a set of overlapping
// intervals into a set of disjoint intervals...
const disjoin = intervals => {
if(intervals.length < 2)
return intervals
const [first, ...rest] = intervals
// ...by recursively injecting each interval into
// an ordered set of disjoint intervals
return insert(first, disjoin(rest))
}
// This function inserts an interval [a,b] into
// an ordered set of disjoint intervals
const insert = ([a, b], intervals) => {
// First we "locate" a and b relative to the interval
// set (before, after, or index of the interval within the set
const pa = pos(a, intervals)
const pb = pos(b, intervals)
// Then we bruteforce all possibilities
if(pa === 'before' && pb === 'before')
return [[a, b], ...intervals]
if(pa === 'before' && pb === 'after')
// ...
if(pa === 'before' && typeof pb === 'number')
// ...
// ... all 6 possibilities
}
const first = intervals => intervals[0][0]
const last = intervals => intervals[intervals.length-1][1]
const pos = (n, intervals) => {
if(n < first(intervals))
return 'before'
if(n > last(intervals))
return 'after'
return intervals.findIndex(([a, b]) => a <= n && n <= b)
}
但是效率很低。在pos 函数中,我可以进行二分搜索以加快速度,但我主要想知道:
- 这是一个已知问题,在算法界有一个名字
- 有一个最佳解决方案与我尝试的方法无关
【问题讨论】:
-
您尝试了什么,遇到了什么问题?
-
我用更多细节更新了我的问题。
-
这是一个非常好的问题。有时间我会回答的。作为提示,我们需要使用不存在的
Array.unfold()。但不用担心。这只是一种从种子值创建数组的方法。
标签: javascript algorithm math intervals