算法 - 查找循环世界中重叠间隔的持续时间(24小时)

时间:2016-01-19 15:45:00

标签: java algorithm intervals overlap

我一直试图找出用于查找两个时间范围之间重叠小时数的算法,例如:

enter image description here

应该返回12。

enter image description here

应该返回4.

所以请帮助我填补创建以下功能的空白:

public static Long findOverlappingInterval(Long startTime1, Long endTime1,
                                           Long startTime2, Long endTime2){ 
    // Any suggestions?
}

感谢。

编辑: 我知道创建两个二进制数组的解决方案,使用AND并总结结果。 含义:

enter image description here

但这对我的特定需求没有帮助,因为我想将算法的思想用于solr查询,所以使用数组和二元运算符不是我的选择

5 个答案:

答案 0 :(得分:3)

令人惊讶的是,很短的时间内答案很多......

我遵循了在其他答案中已经提出的相同想法:当开始时间s小于结束时间e时,结果可以分解为两个单独的计算,范围[s,24][0,e]

这可以“相互”完成,因此只需要考虑3个简单的情况,其余的可以通过递归调用来完成。

然而,我试图

  • 考虑(根据图像),终点应包容(!)
  • 添加更多测试用例
  • 很好地可视化配置: - )

这是MCVE

的结果
public class OverlappingIntervals
{
    private static final long INTERVAL_SIZE = 24;

    public static void main(String[] args)
    {
        test(6,23, 2,17);
        test(0,12, 12,2);

        test(11,4, 12,3);
        test(12,4, 11,3);
    }

    private static void test(
        long s0, long e0, long s1, long e1)
    {
        System.out.println(createString(s0, e0, s1, e1));
        System.out.println(findOverlappingInterval(s0, e0, s1, e1));
    }

    private static String createString(
        long s0, long e0, long s1, long e1)
    {
        StringBuilder sb = new StringBuilder();
        sb.append(createString(s0, e0, "A")).append("\n");
        sb.append(createString(s1, e1, "B"));
        return sb.toString();
    }

    private static String createString(long s, long e, String c)
    {
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<INTERVAL_SIZE; i++)
        {
            if (s < e)
            {
                if (i >= s && i <= e)
                {
                    sb.append(c);
                }
                else
                {
                    sb.append(".");
                }
            }
            else 
            {
                if (i <= e || i >= s)
                {
                    sb.append(c);
                }
                else 
                {
                    sb.append(".");
                }
            }
        }
        return sb.toString();
    }



    public static long findOverlappingInterval(
        long s0, long e0, long s1, long e1)
    {
        return compute(s0, e0+1, s1, e1+1);
    }

    public static long compute(
        long s0, long e0, long s1, long e1)
    {
        if (s0 > e0)
        {
            return 
                compute(s0, INTERVAL_SIZE, s1, e1) +
                compute(0, e0, s1, e1);
        }
        if (s1 > e1)
        {
            return 
                compute(s0, e0, s1, INTERVAL_SIZE) +
                compute(s0, e0, 0, e1);
        }
        return Math.max(0, Math.min(e0, e1) - Math.max(s0, s1));
    }
}

前两个测试用例是问题中给出的测试用例,它们分别正确打印124。其余两个用于测试其他重叠配置:

......AAAAAAAAAAAAAAAAAA
..BBBBBBBBBBBBBBBB......
12
AAAAAAAAAAAAA...........
BBB.........BBBBBBBBBBBB
4
AAAAA......AAAAAAAAAAAAA
BBBB........BBBBBBBBBBBB
16
AAAAA.......AAAAAAAAAAAA
BBBB.......BBBBBBBBBBBBB
16

但请注意,可能必须创建进一步的测试配置才能涵盖所有可能的情况。

答案 1 :(得分:2)

您可以在不创建数组的情况下执行此操作,只计算间隔之间的交叉点。有三种情况:

  1. 没有分割间隔。
  2. 一个分割间隔。
  3. 两个间隔都被拆分。
  4. 您可以将分割间隔的问题解决为两个单独的间隔。使用递归可以轻松完成:

    public static Long findOverlappingInterval(Long startTime1, Long endTime1, Long startTime2, Long endTime2)
    {
        if (startTime1 < endTime1 && startTime2 < endTime2) 
            return Math.max(0, Math.min(endTime2, endTime1) - Math.max(startTime1, startTime2) + 1);
        else
        {
            if (startTime1 < endTime1) 
                return findOverlappingInterval(startTime1, endTime1, 0L, endTime2) + 
                       findOverlappingInterval(startTime1, endTime1, startTime2, 23L);
            else if (startTime2 < endTime2) 
                return findOverlappingInterval(0L, endTime1, startTime2, endTime2) + 
                       findOverlappingInterval(startTime1, 23L, startTime2, endTime2);
            else
            {
                return findOverlappingInterval(0L, endTime1, 0L, endTime2) +
                       findOverlappingInterval(0L, endTime1, startTime2, 23L) +
                       findOverlappingInterval(startTime1, 23L, 0L, endTime2) +
                       findOverlappingInterval(startTime1, 23L, startTime2, 23L);
            }
        }
    }
    

答案 2 :(得分:1)

首先,对于(a, b)的间隔(a > b),我们可以轻松将其分为两个区间

(a , 23) and (0, b)

因此,问题变得找到(a , b)(a1, b1)a <= b and a1 <= b1

之间的重叠

所以,有四种情况:

  • b < a1b1 < a,表示没有重叠,返回0
  • a <= a1 && b1 <= b,表示(a, b)包含(a1, b1),返回b1 - a1 + 1
  • a1 <= a && b <= b1,表示(a1, b1)包含(a, b),返回b - a + 1
  • 最后一种情况是(a, b)(a1, b1)重叠了每个区间的一部分,重叠区间为(max(a1, a), min(b, b1))

答案 3 :(得分:0)

/** Start times must be smaller than end times */
public static int findOverlappingInterval(int startTime1, int endTime1, int startTime2, int endTime2) {
    int overlappingTime = 0;
    int[] time1 = new int[Math.abs(endTime1 - startTime1)];
    for (int i1 = startTime1; i1 < endTime1; i1++) {
        time1[i1 - startTime1] = i1;
    }
    int[] time2 = new int[Math.abs(endTime2 - startTime2)];
    for (int i2 = startTime2; i2 < endTime2; i2++) {
        time2[i2 - startTime2] = i2;
    }

    for (int i = 0; i < time1.length; i++) {
        for (int j = 0; j < time2.length; j++) {
            if (time1[i] == time2[j]) {
                overlappingTime++;
            }
        }
    }
    return overlappingTime;
}

此方法适用于您希望它执行的操作。它对我有用。 我不确定包装部分。

修改

/** Returns the overlap between the four time variables */
public static int findOverlappingInterval(int startTime1, int endTime1, int startTime2, int endTime2) {
    int overlappingTime = 0;

    // First time
    int time1Length = 0;
    if (endTime1 < startTime1) {
        time1Length = 24 - startTime1;
        time1Length += endTime1;
    }
    int[] time1;
    if (time1Length == 0) {
        time1 = new int[Math.abs(endTime1 - startTime1)];
        for (int i1 = startTime1; i1 < endTime1; i1++) {
            time1[i1 - startTime1] = i1;
        }
    } else {
        time1 = new int[time1Length];
        int count = 0;
        for (int i1 = 0; i1 < endTime1; i1++) {
            time1[count] = i1;
            count++;
        }
        for (int i1 = startTime1; i1 < 24; i1++) {
            time1[count] = i1;
            count++;
        }
    }

    // Second time
    int time2Length = 0;
    if (endTime2 < startTime2) {
        time2Length = 24 - startTime2;
        time2Length += endTime2;
    }
    int[] time2;
    if (time2Length == 0) {
        time2 = new int[Math.abs(endTime2 - startTime2)];
        for (int i2 = startTime2; i2 < endTime2; i2++) {
            time2[i2 - startTime2] = i2;
        }
    } else {
        time2 = new int[time2Length];
        int count = 0;
        for (int i2 = 0; i2 < endTime2; i2++) {
            time2[count] = i2;
            count++;
        }
        for (int i2 = startTime2; i2 < 24; i2++) {
            time2[count] = i2;
            count++;
        }
    }

    // Overlap calculator
    for (int i = 0; i < time1.length; i++) {
        for (int j = 0; j < time2.length; j++) {
            if (time1[i] == time2[j]) {
                overlappingTime++;
            }
        }
    }
    return overlappingTime;
}

这个新代码应该做你想要的。它会在24小时内循环。

答案 4 :(得分:0)

点击此链接:Ideone link

  

编辑:从以下链接插入代码:

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static Long min(Long a,Long b){
        return ((a)<(b)?(a):(b));
    }
    public static Long max(Long a,Long b){
        return ((a)>(b)?(a):(b));
    }
    public static void main (String[] args) throws java.lang.Exception
    {
        Long s1=6L,e1=23L,s2=2L,e2=17L,ans=0L;
        Boolean broken1,broken2;
        broken1=(s1<=e1)?false:true;
        broken2=(s2<=e2)?false:true;
        if(broken1){
            if(broken2)
                ans=min(e1,e2)+1 + 23-max(s1,s2)+1;
            else{
                if(e1>=s2) ans+=(min(e1,e2)-s2+1);
                if(s1<=e2) ans+=(e2-max(s1,s2)+1);
            }
        }
        else{
            if(broken2){
                if(e2>=s1) ans+=(min(e1,e2)-s1+1);
                if(s2<=e1) ans+=(e1-max(s1,s2)+1);
            }
            else{
                if(e1<s2 || e2<s1) ans=0L;
                else ans=min(e1,e2)-max(s1,s2)+1;
            }
        }
        System.out.println(ans+"");
    }
}