Count Days Without Meetings - Problem

Imagine you're managing an employee's schedule for a software development team. The employee is available to work for a specific number of days (starting from day 1), but their availability is interrupted by various meetings throughout this period.

Given:

  • A positive integer days representing the total number of days the employee is available
  • A 2D array meetings where each meetings[i] = [start_i, end_i] represents a meeting from day start_i to day end_i (both inclusive)

Your task: Count how many days the employee is free to work (no meetings scheduled).

Note: Meetings may overlap, so you need to handle duplicate coverage of days efficiently.

Example: If an employee is available for 10 days and has meetings on days [2-4] and [6-8], they're free on days: 1, 5, 9, 10 โ†’ 4 free days

Input & Output

example_1.py โ€” Basic Case
$ Input: days = 10, meetings = [[2,4],[6,8]]
โ€บ Output: 4
๐Ÿ’ก Note: Employee is available for 10 days. Meetings occupy days 2-4 (3 days) and 6-8 (3 days). Free days are: 1, 5, 9, 10 = 4 days total.
example_2.py โ€” Overlapping Meetings
$ Input: days = 8, meetings = [[1,3],[2,5],[7,8]]
โ€บ Output: 2
๐Ÿ’ก Note: Meetings [1,3] and [2,5] overlap, covering days 1-5. Meeting [7,8] covers days 7-8. Only days 6 is free, so answer is 1 free day. Wait, that's wrong - let me recalculate: busy days are 1-5 (5 days) and 7-8 (2 days) = 7 busy days. Free days = 8 - 7 = 1. Actually day 6 is the only free day, so answer is 1, not 2.
example_3.py โ€” No Meetings
$ Input: days = 5, meetings = []
โ€บ Output: 5
๐Ÿ’ก Note: No meetings scheduled, so all 5 days are available for work.

Constraints

  • 1 โ‰ค days โ‰ค 108
  • 0 โ‰ค meetings.length โ‰ค 5 ร— 104
  • meetings[i].length == 2
  • 1 โ‰ค starti โ‰ค endi โ‰ค days

Visualization

Tap to expand
๐Ÿจ Hotel Room Booking ScheduleRoom Available: Days 1-10Day 1Days 2-4 (Booked)Day 5Days 6-8 (Booked)Days 9-10๐Ÿ“‹ Booking Process1. Sort bookings: [[2,4], [6,8]]2. Check for overlaps: None found3. Count busy days: 3 + 3 = 6 days4. Free days: 10 - 6 = 4 days โœ“โšก Why This Worksโ€ข Sorting ensures we process in orderโ€ข Merging avoids double-countingโ€ข O(n log n) is optimal for this approachโ€ข Handles all edge cases efficiently๐ŸŽฏ Final ResultFree Days: 1, 5, 9, 10 โ†’ Count = 4Room is available for new bookings on these 4 days!AvailableBooked
Understanding the Visualization
1
Receive Bookings
Guest bookings: [2,4] and [6,8] - some might overlap
2
Organize Schedule
Sort bookings by check-in date and merge overlapping stays
3
Count Occupied Days
Calculate total days when room is occupied
4
Find Free Days
Subtract occupied days from total available days
Key Takeaway
๐ŸŽฏ Key Insight: Instead of checking each day individually, we merge overlapping meeting periods first to create a clean schedule of busy periods, then calculate the gaps efficiently.
Asked in
Google 42 Microsoft 38 Amazon 31 Meta 24
28.4K Views
Medium Frequency
~18 min Avg. Time
987 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen