You are given a positive integer days representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array meetings of size n where, meetings[i] = [start_i, end_i] represents the starting and ending days of meeting i (inclusive).
Return the count of days when the employee is available for work but no meetings are scheduled.
Note: The meetings may overlap.
Example 1:
Input: days = 10, meetings = [[5,7],[1,3],[9,10]]
Output: 2
Explanation:
There is no meeting scheduled on the 4th and 8th days.
Example 2:
Input: days = 5, meetings = [[2,4],[1,3]]
Output: 1
Explanation:
There is no meeting scheduled on the 5th day.
Example 3:
Input: days = 6, meetings = [[1,6]]
Output: 0
Explanation:
Meetings are scheduled for all working days.
Constraints:
1 <= days <= 1091 <= meetings.length <= 105meetings[i].length == 21 <= meetings[i][0] <= meetings[i][1] <= daysWhen you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach is like manually checking every single day on a calendar. We want to find days where there are no meetings. We'll examine each day one by one to see if it has a meeting or not.
Here's how the algorithm would work step-by-step:
def count_days_without_meetings_brute_force(start_date, end_date, meetings):
number_of_free_days = 0
# Iterate through each day in the given range
for current_date in range(start_date, end_date + 1):
has_meeting_today = False
# Check if there's a meeting on the current day
for meeting_date in meetings:
if current_date == meeting_date:
has_meeting_today = True
break
# If no meeting, increment the free days count
if not has_meeting_today:
number_of_free_days += 1
return number_of_free_daysThe problem asks us to find how many days we don't have meetings, given a set of meeting schedules that span across several days. The most efficient approach is to focus on identifying continuous free time blocks, and then add up their lengths.
Here's how the algorithm would work step-by-step:
def count_days_without_meetings(meetings):
all_days = set()
for start_date, end_date in meetings:
for day in range(start_date, end_date + 1):
all_days.add(day)
busy_days = set()
for start_date, end_date in meetings:
for day in range(start_date, end_date + 1):
busy_days.add(day)
free_days_count = 0
current_free_streak = 0
sorted_days = sorted(list(all_days))
# Iterate sorted days to calculate total free days
for day in sorted_days:
if day not in busy_days:
current_free_streak += 1
else:
free_days_count += current_free_streak
current_free_streak = 0
free_days_count += current_free_streak
# This is the final total number of days without meetings
return free_days_count| Case | How to Handle |
|---|---|
| meetings array is null or empty | Return 0, as there are no meetings scheduled, so all days are free. |
| startDay is after endDay | Return 0, as this represents an invalid date range. |
| meetings array contains null meeting objects | Ignore the null meeting objects and continue processing valid meetings. |
| meetings array contains meeting objects where start or end time is outside the startDay and endDay range | Clip the meeting to the startDay and endDay to ensure that we only count meeting days within the range. |
| startDay and endDay are the same day | Check if any meeting overlaps this single day, return 0 if it overlaps and 1 if it does not. |
| Integer overflow if calculating the number of days between startDay and endDay | Use long integer type for days calculation to avoid overflow. |
| Meetings that span multiple days | Iterate from the meeting's start day to its end day and mark each day as a meeting day. |
| Meetings that completely overlap | Union the overlapped meeting days into a single time range to avoid double-counting. |