3169. Count Days Without Meetings

Intitution

We are given a list of meeting intervals, and we want to count how many days remain without any meetings, from day 1 to days. By merging overlapping meetings and tracking gaps between them, we can find all available days.

Complexity

Space Complexity
Time Complexity

O(1)\text{O}(1)

O(NlogN)\text{O}(N*\log{N})

Code

public int countDays(int totalDays, int[][] scheduledMeetings) {
    // Sort meetings by their start time
    Arrays.sort(scheduledMeetings, (a, b) -> Integer.compare(a[0], b[0]));

    int availableDays = 0;
    int lastBusyDay = 0;  // Tracks end of last merged meeting range

    for (int i = 0; i < scheduledMeetings.length; ++i) {
        int currentStart = scheduledMeetings[i][0];
        int currentEnd = scheduledMeetings[i][1];

        // If there's a gap between the last meeting and current one, add it
        if (currentStart > lastBusyDay) {
            availableDays += (currentStart - lastBusyDay - 1);
        }

        // Update lastBusyDay to the farthest end we've seen so far
        lastBusyDay = Math.max(lastBusyDay, currentEnd);
    }

    // Account for any remaining days after the last meeting
    if (totalDays > lastBusyDay) {
        availableDays += (totalDays - lastBusyDay);
    }

    return availableDays;
}

Last updated