查找会话重叠

时间:2017-02-05 21:27:09

标签: python session tree intervals

我有一个包含开始和结束时间戳的任意数量的会话

其中一些会话重叠。多个会话可以同时重叠。

我正在尝试找到一种可以检测重叠秒数的算法。 IE给出了3个会话,如

-ID-|-start-|-end-|
--1-|-----4-|--10-|
--2-|-----5-|--12-|
--3-|-----8-|--13-|

让它返回一个数字,即会话重叠的秒数。

我已阅读interval trees并查看了this one等python包。

但是,我不确定如何获得给定记录集的重叠秒数。你知道算法或包吗? Python首选但对其他语言开放,我可以重新实现。

1 个答案:

答案 0 :(得分:1)

我想到的第一个想法是复杂的O(n log n)用于排序。如果已经对startsends进行了排序,则算法的复杂度为O(n)。

int findOverlappingTimes(int[] starts, int ends[]) {

    // TODO: Sort starts array
    // TODO: Sort ends array
    // TODO: Assert starts.length == ends.length

    int currStartsIndex = 0;
    int currEndsIndex = 0;

    int currOverlaps = 0;
    int lastOverlapIndex = -1;

    int result = 0;

    while (currEndsIndex < ends.length) {

        if (currStartsIndex < starts.length && starts[currStartsIndex] < ends[currEndsIndex]) {
            if (++currOverlaps == 2) { // Start counting if at least two intervals overlap
                lastOverlapIndex = currStartsIndex;
            }
            currStartsIndex++;
        } else {
            if (--currOverlaps <= 1 && lastOverlapIndex != -1) { // Stop counting
                result += ends[currEndsIndex] - starts[lastOverlapIndex];
                lastOverlapIndex = -1;
            }
            currEndsIndex++;
        }

    }

    return result;
}

输入集的输出

findOverlappingTimes(new int[] { 4, 5, 8 }, new int[] { 10, 12, 13 })

返回7

算法背后的基本思想是迭代会话并计算当前重叠会话的数量。如果当前时间至少有两个会话重叠,我们开始计算重叠时间,如果重叠结束则停止计算重叠时间。

以下是一些测试用例及其各自的输出:

findOverlappingTimes(new int[] { 0 }, new int[] { 0 }) = 0
findOverlappingTimes(new int[] { 10 }, new int[] { 10 }) = 0
findOverlappingTimes(new int[] { 10 }, new int[] { 20 }) = 0
findOverlappingTimes(new int[] { 10, 10 }, new int[] { 10, 10 }) = 0
findOverlappingTimes(new int[] { 10, 10 }, new int[] { 11, 11 }) = 1
findOverlappingTimes(new int[] { 10, 10, 10 }, new int[] { 11, 11, 12 }) = 1
findOverlappingTimes(new int[] { 10, 10, 10, 50, 90, 110 }, new int[] { 11, 12, 12, 100, 150, 160 }) = 52
findOverlappingTimes(new int[] { 4, 5, 8, 100, 200, 200, 300, 300 }, new int[] { 10, 12, 13, 110, 200, 200, 320, 330 }) = 27