You are given an integer eventTime denoting the duration of an event, where the event occurs from time t = 0 to time t = eventTime.
You are also given two integer arrays startTime and endTime, each of length n. These represent the start and end time of n non-overlapping meetings, where the ith meeting occurs during the time [startTime[i], endTime[i]].
You can reschedule at most k meetings by moving their start time while maintaining the same duration, to maximize the longest continuous period of free time during the event.
The relative order of all the meetings should stay the same and they should remain non-overlapping.
Return the maximum amount of free time possible after rearranging the meetings.
Note that the meetings can not be rescheduled to a time outside the event.
Example 1:
Input: eventTime = 5, k = 1, startTime = [1,3], endTime = [2,5]
Output: 2
Explanation:

Reschedule the meeting at [1, 2] to [2, 3], leaving no meetings during the time [0, 2].
Example 2:
Input: eventTime = 10, k = 1, startTime = [0,2,9], endTime = [1,4,10]
Output: 6
Explanation:

Reschedule the meeting at [2, 4] to [1, 3], leaving no meetings during the time [3, 9].
Example 3:
Input: eventTime = 5, k = 2, startTime = [0,1,2,3,4], endTime = [1,2,3,4,5]
Output: 0
Explanation:
There is no time during the event not occupied by meetings.
Constraints:
1 <= eventTime <= 109n == startTime.length == endTime.length2 <= n <= 1051 <= k <= n0 <= startTime[i] < endTime[i] <= eventTimeendTime[i] <= startTime[i + 1] where i lies in the range [0, n - 2].When 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 strategy involves trying every possible combination of rescheduling one meeting. For each potential schedule, we calculate the amount of free time and pick the schedule that gives us the most free time.
Here's how the algorithm would work step-by-step:
def reschedule_meetings_brute_force(meetings): best_schedule = meetings
max_free_time = calculate_free_time(meetings)
for meeting_index in range(len(meetings)):
original_meeting = meetings[meeting_index]
# Iterate through possible start times
for new_start_time in range(0, 24):
# Create a temporary meeting schedule.
temp_meetings = meetings[:]
# Attempt to reschedule current meeting.
temp_meeting = (new_start_time, new_start_time + (original_meeting[1] - original_meeting[0]))
temp_meetings[meeting_index] = temp_meeting
# Calculate free time to check the schedule.
current_free_time = calculate_free_time(temp_meetings)
# Find the schedule with the maximum free time
if current_free_time > max_free_time:
# Update max free time and save the new schedule.
max_free_time = current_free_time
best_schedule = temp_meetings[:]
return best_schedule
def calculate_free_time(meetings):
meetings.sort()
total_free_time = 0
end_time = 0
for meeting in meetings:
start_time = meeting[0]
meeting_end_time = meeting[1]
# Calculate gaps between meetings.
if start_time > end_time:
total_free_time += start_time - end_time
end_time = max(end_time, meeting_end_time)
total_free_time += (24 - end_time)
return total_free_timeTo maximize free time, we need to find the biggest gap between meetings. First, sort the meetings by start time. Then, calculate the duration of the gaps between consecutive meetings.
Here's how the algorithm would work step-by-step:
def find_max_free_time(meetings):
# Sort the meetings by start time to easily find gaps.
meetings.sort(key=lambda meeting: meeting[0])
max_free_time = 0
# Iterate through meetings to find the largest gap.
for i in range(1, len(meetings)):
previous_meeting_end = meetings[i - 1][1]
current_meeting_start = meetings[i][0]
# Calculate the gap between consecutive meetings.
free_time_duration = current_meeting_start - previous_meeting_end
# Update max_free_time if the current gap is larger.
max_free_time = max(max_free_time, free_time_duration)
return max_free_time| Case | How to Handle |
|---|---|
| Empty meeting list | Return an empty list since no meetings exist to reschedule, and therefore no free time can be generated. |
| Meetings overlap completely (A encompasses B) | The merging/sorting logic must correctly handle cases where one meeting interval completely contains another, combining them into one larger interval. |
| Adjacent meetings with no gap | The merging logic should combine adjacent meetings into a single continuous interval to avoid zero-length free time slots. |
| Meetings are already perfectly scheduled (no overlap, sorted) | The algorithm should still correctly identify and return the free time between the existing meeting intervals without modification. |
| Meetings start at the same time, different end times | Sorting needs to properly prioritize meetings with earlier start times and, when equal, consider earlier end times to ensure correct merging. |
| Integer overflow when calculating meeting duration/time differences | Use appropriate data types (long) or modular arithmetic if necessary, and consider the range of input values to prevent overflow errors. |
| Maximum number of meetings close to the memory limit | Consider using more memory-efficient data structures or algorithms to avoid exceeding memory constraints when handling a very large number of meetings. |
| Meetings with identical start and end times (zero-length meeting) | Treat these as valid meetings and either merge them away or ignore them during free time calculations. |