Блог пользователя Betlista

Автор Betlista, 9 лет назад, По-английски

This is just a note for me, to not "invent" it in next contests, maybe too easy for others.

I have two intervals [f1, t1] and [f2, t2] and I want to find intersection.

private static int[] intersect(int f1, int t1, int f2, int t2) {
    if ( isBetween(f1, f2, t2) ) {
        return new int[] { f1, Math.min(t1, t2) };
    } else if ( isBetween(f2, f1, t1) ) {
        return new int[] { f2, Math.min(t1, t2) };
    } else {
        return null;
    }
}

private static boolean isBetween(int n, int f, int t) {
    return f <= n && n <= t;
}
  • Проголосовать: нравится
  • +8
  • Проголосовать: не нравится

»
9 лет назад, # |
Rev. 2   Проголосовать: нравится +41 Проголосовать: не нравится
private static int[] intersect(int f1, int t1, int f2, int t2) {
    int f = Math.max(f1, f2), t = Math.min(t1, t2);
    if (f <= t) {
        return new int[]{f, t};
    } else {
        return null;
    }
}