查找区间交点的算法

时间:2019-06-24 07:59:59

标签: java algorithm

我有两个列表,每组间隔。我想找到两个列表的交集。

Eg.
List1 = [{1, 10}, {15, 25}, {30, 32}];
List2 = [{3,5}, {9,14}, {17,20}];

结果:

IntersectionList = [{3,5}, {9,10}, {17,20}];

尝试使用: Java algorithm for find intersection between intervals,但是在第一个列表中只有一个对象。

我希望能够在每个列表中提供多个间隔对象。

public class CalendarTimeSlot {
    private long from;
    private long to;
}

public class IntersectIntervalsHelper {

        public static void main(String[] args) {

        List<CalendarTimeSlot> l1 = new ArrayList<>();
        l1.add(new CalendarTimeSlot(1, 10));
        l1.add(new CalendarTimeSlot(15, 25));
        l1.add(new CalendarTimeSlot(30, 32));

        List<CalendarTimeSlot> l2 = new ArrayList<>();
        l2.add(new CalendarTimeSlot(3, 5));
        l2.add(new CalendarTimeSlot(9, 14));
        l2.add(new CalendarTimeSlot(17, 20));

        //todo code here to do l1 intersection l2 slots
    }

}

示例输入:

List1 = [{1, 10}, {15, 25}, {30, 32}];
List2 = [{3,5}, {9,14}, {17,20}];

结果:

IntersectionList = [{3,5}, {9,10}, {17,20}];

3 个答案:

答案 0 :(得分:2)

忽略两个对并确保它们已排序,从而平整两个列表。您将获得两个数字列表,以便每个间隔都包含在偶数和奇数索引之间(从零开始)。

现在考虑一个标准的合并过程,该过程将生成第三个列表。

1, 10, 15, 25, 30, 32
3, 5, 9, 14, 17, 20

给予

1, 3, 5, 9, 10, 14, 15, 17, 20, 25, 30, 32

同时,您可以为列表添加注释,以表明您在这两个列表之内还是之外。

  1, 3, 5, 9, 10, 14, 15, 17, 20, 25, 30, 32
oo io ii io ii  oi  oo  io  ii  io  oo  io  oo  

所需的输出由ii个间隔组成,

  3, 5, 9, 10, 17, 20
   ii    ii      ii  

答案 1 :(得分:0)

制作一个包含所有间隔结束的对的单个列表。
每对都是<div class="container"> <div class="row"> <div class="col-md-3 sidebar"> content </div> <div class="col-md-9 main-content"> content </div> </div> </div>
按值对列表进行排序。
制作(value; +1/-1 for interval start/end)
在排序列表中运行,并以{/ {1}}开始/结束标记递增。
ActiveCount=0变为2时,输出间隔开始。
ActiveCount变为1(2!之后)时,输出间隔结束。

答案 2 :(得分:0)

public static List<CalendarTimeSlot> createIntersection(List<CalendarTimeSlot> l1, List<CalendarTimeSlot> l2) {
    List<CalendarTimeSlot> intersectedSlots = new ArrayList<>();
    int length1 = l1.size();
    int length2 = l2.size();
    int l1Counter = 0, l2Counter = 0;
    while (l1Counter<length1 && l2Counter<length2) {
        CalendarTimeSlot calendarTimeSlot1 = l1.get(l1Counter);
        CalendarTimeSlot calendarTimeSlot2 = l2.get(l2Counter);

        long from = 0, to =0;
        if(calendarTimeSlot1.getFrom()<=calendarTimeSlot2.getFrom()) {
            from = calendarTimeSlot2.getFrom();

            //smaller on is the "to"
            if(calendarTimeSlot1.getTo()>=calendarTimeSlot2.getTo()) {
                to = calendarTimeSlot2.getTo();
            } else {
                to = calendarTimeSlot1.getTo();
            }
        } else {
            //l1's from is greater
            if(calendarTimeSlot2.getTo()>calendarTimeSlot1.getFrom()) {
                from = calendarTimeSlot1.getFrom();

                //smaller on is the "to"
                if(calendarTimeSlot1.getTo()>=calendarTimeSlot2.getTo()) {
                    to = calendarTimeSlot2.getTo();
                } else {
                    to = calendarTimeSlot1.getTo();
                }
            }
        }

        if(calendarTimeSlot1.getTo()<calendarTimeSlot2.getTo()) {
            l1Counter++;
        } else {
            l2Counter++;
        }

        if(from>0 && to>0) {
            intersectedSlots.add(new CalendarTimeSlot(from, to));
        }

    }

    return intersectedSlots;
}