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:
Imagine you have a calendar full of meetings and you are allowed to shift a certain number of them around to create the longest possible stretch of uninterrupted free time. The brute force strategy works by systematically trying every single combination of moving different meetings to different time slots and measuring the resulting gaps.
Here's how the algorithm would work step-by-step:
def max_free_time(event_duration, workday_limit, meeting_starts, meeting_ends):
# Generate all combinations of meetings to evaluate every possible rescheduling permutation
from itertools import combinations
maximum_free_gap = 0
total_meetings = len(meeting_starts)
for moving_count in range(min(event_duration, total_meetings) + 1):
for chosen_indices in combinations(range(total_meetings), moving_count):
# Reconstruct the schedule by removing chosen meetings and compressing the remaining ones
remaining_starts = [meeting_starts[index] for index in range(total_meetings) if index not in chosen_indices]
remaining_ends = [meeting_ends[index] for index in range(total_meetings) if index not in chosen_indices]
current_position = 0
merged_intervals = []
for start_time, end_time in zip(remaining_starts, remaining_ends):
duration = end_time - start_time
merged_intervals.append((current_position, current_position + duration))
current_position += duration
# Calculate all gaps in the newly formed schedule to find the maximum uninterrupted time
all_gaps = []
previous_end = 0
for interval_start, interval_end in merged_intervals:
all_gaps.append(interval_start - previous_end)
previous_end = interval_end
all_gaps.append(workday_limit - previous_end)
if all_gaps:
maximum_free_gap = max(maximum_free_gap, max(all_gaps))
return maximum_free_gapInstead of testing every possible way to move meetings, the optimal approach focuses on finding the longest continuous stretch of free time you can create by shifting a set number of meetings together. It uses a sliding window technique to evaluate groups of adjacent gaps efficiently.
Here's how the algorithm would work step-by-step:
def max_free_time(event_horizon, meeting_start_times, meeting_end_times):
# Collect all free time intervals including boundaries before and after all meetings.
free_time_gaps = [meeting_start_times[0]]
for meeting_index in range(1, len(meeting_start_times)):
current_gap = meeting_start_times[meeting_index] - meeting_end_times[meeting_index - 1]
free_time_gaps.append(current_gap)
free_time_gaps.append(event_horizon - meeting_end_times[-1])
window_size = event_limit + 1
# Sum the initial window of gaps and the meetings that fall between them.
current_window_sum = sum(free_time_gaps[:window_size])
maximum_free_time = current_window_sum
# Slide the window across all gaps to evaluate different merge combinations efficiently.
for gap_index in range(window_size, len(free_time_gaps)):
current_window_sum += free_time_gaps[gap_index] - free_time_gaps[gap_index - window_size]
if current_window_sum > maximum_free_time:
maximum_free_time = current_window_sum
return maximum_free_time| Case | How to Handle |
|---|---|
| k is greater than or equal to the total number of meetings | The entire event time becomes a single continuous free block since all meetings can be shifted out of the way. |
| k equals 0, meaning no meetings can be rescheduled | The algorithm simply finds the maximum gap between the already fixed meetings and the boundaries. |
| Meetings already span the entire event time with zero free space initially | Shifting meetings allows consolidating free spaces if k is greater than zero, or returns zero if k is zero. |
| All meetings are packed consecutively with no gaps between them | Rescheduling a window of k meetings creates a large free block by shifting them to either end of the timeline. |
| Large scale inputs with maximum possible constraints on eventTime and meeting counts | A sliding window approach running in O(n) time ensures efficient processing without exceeding time limits. |
| Time values exceed standard 32-bit integer limits | Using 64-bit integers for all time calculations prevents potential overflow issues during subtractions. |
| Unsorted start and end time arrays | The algorithm must initially sort the meetings by start time to accurately compute existing gaps. |