To find all overlapping date ranges from this list of date ranges

I have a BookingDateRange list where BookingDateRange:

    public class BookingDateRange {
        private Date fromDate;
        private Date toDate;

        //getters & setters of properties
   }

Requirements:

  • I need to find if any dates in BookingDate list match, date DateRangeList
  • If yes, find all pairs of date ranges that overlap, say List of String says overlappingDatePairs

Example 1 :

Input1:

dateRangeList [0] = Dec 23, 2012 - Dec 27, 2012

dateRangeList [1] = December 14, 2012 - Dec 25, 2012

dateRangeList [2] = 1 jan 2012 - 23 Jan 2012

Output1:

isOverlappingDates = true

overlappingDatePairs = [0_1]

Example 2:

Input2:

dateRangeList [0] = Dec 23, 2012 - Dec 27, 2012

dateRangeList [1] = 1 jan 2012 - 23 Jan 2012

Output2:

isOverlappingDates = false

overlappingDatePairs = []

My decision:

/**
 * Checks if any of the dates overlap.
 *
 * @param dateRangeList the date range list
 * @param overlappingDatePairs the overlapping date pairs where overlappingDatePair is stored in the format dateRange1_dateRange2
 * @return true, if any of the dates overlap.
 */

public static boolean isOverlappingDates(
            List<BookingDateRange> dateRangeList,
            List<String> overlappingDatePairs) {

    boolean isOverlap = false;

    for (int index1 = 0; index1 < dateRangeList.size(); index1++) {
        for (int index2 = index1 + 1; index2 < dateRangeList.size(); index2++) {

            // Overlap exists if (StartA <= EndB) and (EndA >= StartB)

            Date startA = dateRangeList.get(index1).getFromDate();
            Date endA = dateRangeList.get(index1).getToDate();
            Date startB = dateRangeList.get(index2).getFromDate();
            Date endB = dateRangeList.get(index2).getToDate();

            boolean isStartABeforeEndB = (startA.compareTo(endB)) < 0;
            boolean isEndAAfterStartB = (endA.compareTo(startB)) > 0;

            boolean isCurrentPairOverlap = false;

            isCurrentPairOverlap = isStartABeforeEndB && isEndAAfterStartB;

            if (isCurrentPairOverlap) {
                overlappingDatePairs.add(index1 + "_" + index2);
                isOverlap = true;
            }
        }

    }
    return isOverlap;

    }

O (n ^ 2). ? .

SO. .

, Shikha

+5
3

O (nlog (n)), , , , O ( ). , , .

private static class BookingTuple implements Comparable<BookingTuple> {
    public final Date date;
    public final boolean isStart;
    public final int id;
    public BookingTuple(Date date, boolean isStart, int id) {
        this.date = date;
        this.isStart = isStart;
        this.id = id;
    }

    @Override
    public int compareTo(BookingTuple other) {
        int dateCompare = date.compareTo(other.date);
        if (dateCompare != 0) {
            return dateCompare;
        } else {
            if (!isStart && other.isStart) {
                return -1;
            } else if (isStart && !other.isStart) {
                return 1;
            } else {
                return 0;
            }
        }
    }
}

public static boolean isOverlappingDates(List<BookingDateRange> dateRangeList, List<String> overlappingDatePairs) {
    List<BookingTuple> list = new ArrayList<BookingTuple>();
    for (int i = 0; i < dateRangeList.size(); i++) {
        Date from = dateRangeList.get(i).getFromDate();
        Date to = dateRangeList.get(i).getToDate();
        list.add(new BookingTuple(from, true, i));
        list.add(new BookingTuple(to, false, i));
    }

    Collections.sort(list);

    boolean overlap = false;

    HashSet<Integer> active = new HashSet<Integer>();
    for (BookingTuple tuple : list) {
        if (!tuple.isStart) {
            active.remove(tuple.id);
        } else {
            for (Integer n : active) {
                overlappingDatePairs.add(n + "_" + tuple.id);
                overlap = true;
            }
            active.add(tuple.id);
        }
    }

    return overlap;
}
+4

, , BookingDateRange ...

, O(n) comparisons per element, n elements

n * n = O(n^2)

+3

. , ( 5000BC 6000AD).

1 Map<String, List<Range>>.

2 GregorianCalendar. "yyyy/MM/dd" ( ).

2a If the line "yyyy / MM / dd" is already on the map, add the new range to the list.

2b If this is not the case, add an entry with a new list containing the range.

2c Increase GregorianCalendar per day. Repeat until the last date in the range is reached.

3 Repeat all ranges.

4 List the keys of the card (if you used the sorting format "yyyy / MM / dd", trivial). Check the size of the linked list.

0
source

All Articles