8

我有两个间隔列表。我想从 list2 中已经存在的 list1 中删除所有时间。示例:列表 1:

[(0,10),(15,20)]

清单 2:

[(2,3),(5,6)]

输出:

[(0,2),(3,5),(6,10),(15,20)]

有什么提示吗?

当时试图删除一个间隔,但似乎我需要采取不同的方法:

public List<Interval> removeOneTime(Interval interval, Interval remove){
    List<Interval> removed = new LinkedList<Interval>();
    Interval overlap = interval.getOverlap(remove);
    if(overlap.getLength() > 0){
        List<Interval> rms = interval.remove(overlap);
        removed.addAll(rms);
    }
    return removed;
}
4

2 回答 2

7

我会用扫描线算法来解决这个问题。间隔的起点和终点是事件,它们被放入优先级队列中。您只需从左到右移动,在每个事件处停止,并根据该事件更新当前状态。

我做了一个小实现,为了简单起见,我在其中使用了以下Interval类:

public class Interval {
    public int start, end;

    public Interval(int start, int end) {       
        this.start = start;
        this.end = end;
    }

    public String toString() {
        return "(" + start + "," + end + ")";
    }
}

前面提到的事件点由以下类表示:

public class AnnotatedPoint implements Comparable<AnnotatedPoint> {
    public int value;
    public PointType type;

    public AnnotatedPoint(int value, PointType type) {
        this.value = value;
        this.type = type;
    }

    @Override
    public int compareTo(AnnotatedPoint other) {
        if (other.value == this.value) {
            return this.type.ordinal() < other.type.ordinal() ? -1 : 1;
        } else {
            return this.value < other.value ? -1 : 1;
        }
    }

    // the order is important here: if multiple events happen at the same point,
    // this is the order in which you want to deal with them
    public enum PointType {
        End, GapEnd, GapStart, Start
    }
}

现在,剩下的就是构建队列并进行扫描,如下面的代码所示

public class Test {

    public static void main(String[] args) {        
        List<Interval> interval = Arrays.asList(new Interval(0, 10), new Interval(15, 20));
        List<Interval> remove = Arrays.asList(new Interval(2, 3), new Interval(5, 6));

        List<AnnotatedPoint> queue = initQueue(interval, remove);       
        List<Interval> result = doSweep(queue);

        // print result
        for (Interval i : result) {
            System.out.println(i);
        }
    }

    private static List<AnnotatedPoint> initQueue(List<Interval> interval, List<Interval> remove) {
        // annotate all points and put them in a list
        List<AnnotatedPoint> queue = new ArrayList<>();
        for (Interval i : interval) {
            queue.add(new AnnotatedPoint(i.start, PointType.Start));
            queue.add(new AnnotatedPoint(i.end, PointType.End));
        }
        for (Interval i : remove) {
            queue.add(new AnnotatedPoint(i.start, PointType.GapStart));
            queue.add(new AnnotatedPoint(i.end, PointType.GapEnd));
        }

        // sort the queue
        Collections.sort(queue);

        return queue;
    }

    private static List<Interval> doSweep(List<AnnotatedPoint> queue) {
        List<Interval> result = new ArrayList<>();

        // iterate over the queue       
        boolean isInterval = false; // isInterval: #Start seen > #End seen
        boolean isGap = false;      // isGap:      #GapStart seen > #GapEnd seen
        int intervalStart = 0;  
        for (AnnotatedPoint point : queue) {
            switch (point.type) {
            case Start:
                if (!isGap) {                   
                    intervalStart = point.value;
                }
                isInterval = true;
                break;
            case End:
                if (!isGap) {                   
                    result.add(new Interval(intervalStart, point.value));
                }
                isInterval = false;
                break;
            case GapStart:
                if (isInterval) {                   
                    result.add(new Interval(intervalStart, point.value));
                }               
                isGap = true;
                break;
            case GapEnd:
                if (isInterval) {
                    intervalStart = point.value;
                }
                isGap = false;
                break;
            }
        }

        return result;
    }
}

这导致:

(0,2)
(3,5)
(6,10)
(15,20)
于 2013-04-30T17:05:45.020 回答
1

您可能想要使用区间树- 这将快速告诉您区间是否与树中的任何区间重叠。

一旦你有一组重叠间隔,任务应该相当容易(interval1来自list1,interval2是list2 /间隔树的重叠间隔):如果interval1包含interval2,那么你有两个新间隔(interval1min,interval2min), (interval2max, interval1max); 如果interval1不包含interval2,那么你只有一个新的间隔(interval1min,interval2min)或(interval2max,interval1max)

于 2013-04-30T16:07:16.587 回答