You are given an integer eventTime denoting the duration of an event. You are also given two integer arrays startTime and endTime, each of length n.
These represent the start and end times of n non-overlapping meetings that occur during the event between time t = 0 and time t = eventTime, where the ith meeting occurs during the time [startTime[i], endTime[i]].
You can reschedule at most one meeting by moving its start time while maintaining the same duration, such that the meetings remain non-overlapping, to maximize the longest continuous period of free time during the event.
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 and they should remain non-overlapping.
Note: In this version, it is valid for the relative ordering of the meetings to change after rescheduling one meeting.
Example 1:
Input: eventTime = 5, 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, startTime = [0,7,9], endTime = [1,8,10]
Output: 7
Explanation:

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

Reschedule the meeting at [3, 4] to [8, 9], leaving no meetings during the time [1, 7].
Example 4:
Input: eventTime = 5, 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 <= 1050 <= startTime[i] < endTime[i] <= eventTimeendTime[i] <= startTime[i + 1] where i lies in the range [0, n - 2].Problem Overview: You are given a list of non-overlapping meetings inside a fixed event timeline. You can reschedule exactly one meeting (keeping its duration the same) to any valid empty slot. The goal is to maximize the longest continuous free time interval after the reschedule.
Approach 1: Enumeration / Brute Force (O(n^2) time, O(1) space)
Try moving every meeting and evaluate the best place to relocate it. For meeting i, temporarily remove it and compute the merged free gap created between its neighbors. Then iterate through every other free gap to see if the meeting’s duration fits there. If it fits, you can place the meeting in that gap and keep the newly merged gap as free time. This approach relies on array iteration and explicit gap checking. It works for small inputs but becomes expensive because every meeting scans all other gaps.
Approach 2: Greedy with Gap Tracking (O(n) time, O(n) space)
Instead of recomputing gaps repeatedly, precompute all free gaps between meetings. Let gap[i] represent the free time before meeting i. When you remove meeting i, the gap between its neighbors becomes gap[i] + duration[i] + gap[i+1]. The key question: can the removed meeting fit somewhere else so this merged gap remains free?
Track the largest gaps to the left and right of each meeting using prefix and suffix maximum arrays. These allow constant-time checks for the largest available gap that does not touch meeting i. If any of those gaps can accommodate the meeting duration, the merged gap becomes the candidate free time. Otherwise, the meeting must be placed back inside the merged gap, reducing the free interval. This greedy observation avoids scanning all gaps repeatedly and turns the solution into a linear pass.
The algorithm mainly uses greedy reasoning with precomputed arrays and simple array traversal. Each meeting is evaluated once, and gap feasibility checks happen in O(1).
Recommended for interviews: Start by explaining the enumeration idea to show you understand how rescheduling affects neighboring gaps. Then move to the greedy optimization with prefix/suffix maximum gaps. Interviewers typically expect the O(n) solution because it demonstrates pattern recognition around gap merging, precomputation, and efficient enumeration.
According to the problem description, for meeting i, let l_i be the non-free position to its left, r_i be the non-free position to its right, and let the duration of meeting i be w_i = endTime[i] - startTime[i]. Then:
$
l_i = \begin{cases}
0 & i = 0 \\
endTime[i - 1] & i > 0
\end{cases}
r_i = \begin{cases}
eventTime & i = n - 1 \\
startTime[i + 1] & i < n - 1
\end{cases}
The meeting can be moved to the left or right, and the free time in this case is:
r_i - l_i - w_i
If there exists a maximum free time on the left, pre_{i - 1}, such that pre_{i - 1} geq w_i, then meeting i can be moved to that position on the left, resulting in a new free time:
r_i - l_i
Similarly, if there exists a maximum free time on the right, suf_{i + 1}, such that suf_{i + 1} geq w_i, then meeting i can be moved to that position on the right, resulting in a new free time:
r_i - l_i
Therefore, we first preprocess two arrays, pre and suf, where pre[i] represents the maximum free time in the range [0, i], and suf[i] represents the maximum free time in the range [i, n - 1]. Then, for each meeting i, we calculate the maximum free time after moving it, and take the maximum value.
The time complexity is O(n), and the space complexity is O(n), where n$ is the number of meetings.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Enumeration / Brute Force | O(n^2) | O(1) | Useful for reasoning about how removing and relocating a meeting changes surrounding gaps |
| Greedy with Prefix/Suffix Max Gaps | O(n) | O(n) | Best for large inputs; evaluates each meeting once using precomputed largest gaps |
Reschedule Meetings for Maximum Free Time II | Detailed Intuition | Leetcode 3440 | codestorywithMIK • codestorywithMIK • 11,242 views views
Watch 9 more video solutions →Practice Reschedule Meetings for Maximum Free Time II with our built-in code editor and test cases.
Practice on FleetCode