Count Days Without Meetings (LeetCode 3169): Solution and Explanation
Given a number of working days and a list of possibly overlapping meetings (inclusive day ranges), count the days that have no meeting at all.
Intuition
days can be up to 10^9, so marking a calendar day by day is out. The answer depends only on where the meeting intervals leave holes, and there are at most 10^5 intervals.
Sort the meetings by start day and sweep left to right. Keep last_end, the last day that is already covered by some meeting seen so far. Every day up to last_end is settled: either it was counted as free or it is busy.
When the next meeting starts at start > last_end + 1, the days last_end + 1 through start - 1 belong to no meeting, because every later meeting starts even later. That gap holds start - last_end - 1 free days.
Then extend last_end to max(last_end, end). Taking the max matters: a meeting nested inside an earlier long one must not pull last_end backwards. After the sweep, days last_end + 1 through days are free too.
Approach: Sort and sweep with the last covered day
- Sort meetings by start day.
- Set free_days = 0 and last_end = 0.
- For each meeting [start, end]: if start > last_end + 1, add start - last_end - 1 to free_days. Then last_end = max(last_end, end).
- Add days - last_end for the stretch after the last meeting.
- Return free_days.
Step-by-step example
days = 10, meetings = [[5,7],[1,3],[9,10]]
- Sorted: [1,3], [5,7], [9,10]. last_end = 0.
- [1,3] starts right after day 0, no gap. last_end = 3.
- [5,7] starts at 5 > 4, so day 4 is free. free_days = 1, last_end = 7.
- [9,10] starts at 9 > 8, so day 8 is free. free_days = 2, last_end = 10.
- days - last_end = 0, so the answer is 2.
Code: Python, Java, C++
1class Solution:
2 def countDays(self, days: int, meetings: List[List[int]]) -> int:
3 meetings.sort()
4 free_days = 0
5 last_end = 0
6 for start, end in meetings:
7 if start > last_end + 1:
8 free_days += start - last_end - 1
9 last_end = max(last_end, end)
10 free_days += days - last_end
11 return free_days1class Solution {
2 public int countDays(int days, int[][] meetings) {
3 Arrays.sort(meetings, (a, b) -> Integer.compare(a[0], b[0]));
4 int freeDays = 0, lastEnd = 0;
5 for (int[] m : meetings) {
6 if (m[0] > lastEnd + 1) {
7 freeDays += m[0] - lastEnd - 1;
8 }
9 lastEnd = Math.max(lastEnd, m[1]);
10 }
11 freeDays += days - lastEnd;
12 return freeDays;
13 }
14}1class Solution {
2public:
3 int countDays(int days, vector<vector<int>>& meetings) {
4 sort(meetings.begin(), meetings.end());
5 int freeDays = 0, lastEnd = 0;
6 for (const auto& m : meetings) {
7 if (m[0] > lastEnd + 1) {
8 freeDays += m[0] - lastEnd - 1;
9 }
10 lastEnd = max(lastEnd, m[1]);
11 }
12 freeDays += days - lastEnd;
13 return freeDays;
14 }
15};Time and space complexity
O(n log n)
Sorting dominates; the sweep is one pass.
O(1)
Only two counters beyond the sort's own memory.
Other approaches
Merge intervals, then subtract (time O(n log n), space O(n))
Merge the meetings into disjoint blocks, sum their lengths, and subtract from days. Same complexity, with an extra list of merged blocks.
Difference array (time O(n + days), space O(days))
Add +1 at each start and -1 after each end, then scan the days counting zeros. Only works when days is small, which it is not here.
Common mistakes
- Setting last_end = end instead of max(last_end, end). A short meeting inside a long one would shrink the covered range and create fake free days.
- Off-by-one on inclusive ranges. A meeting starting at last_end + 1 leaves no gap; the gap size is start - last_end - 1.
- Forgetting the free days after the final meeting.
Frequently asked questions
Why is sorting by start enough?
Once meetings are in start order, any day before the current start that is not covered by an earlier meeting cannot be covered by a later one either, because later meetings start even later.
Can the answer overflow a 32-bit int?
No. It is at most days, which is at most 10^9.
How is this related to Merge Intervals?
It is the same sweep. Instead of outputting the merged blocks, it counts the holes between them.
What is the complexity?
O(n log n) time and O(1) extra space.