Watch 10 video solutions for Reschedule Meetings for Maximum Free Time II, a medium level problem involving Array, Greedy, Enumeration. This walkthrough by codestorywithMIK has 11,242 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |