Problem Description
Given a total number of days and a list of meeting intervals (which may overlap), determine how many days are free (i.e. days where no meeting is scheduled).
Key Insights
- Because meetings may overlap, merge overlapping intervals to avoid counting the same day more than once.
- Instead of tracking each individual day (which is not feasible for days up to 10^9), calculate the total days covered by the merged intervals.
- Sort the intervals by start day to simplify the merging process.
- Subtract the total days occupied by meetings from the total available days.
Space and Time Complexity
Time Complexity: O(n log n) due to sorting the meeting intervals. Space Complexity: O(n) for storing the merged intervals.
Solution
The solution involves first sorting the meetings by their start day. Then, iterate through the sorted intervals to merge overlapping or contiguous meeting intervals. Once all intervals are merged, compute the number of days covered by these intervals. Finally, subtract the covered days from the total days to get the number of free days. This approach ensures that we efficiently handle large input sizes without iterating over every day.