Problem Description
You are given the total duration of an event (eventTime) and n non‐overlapping meetings represented by their start and end times. You are allowed to reschedule at most one meeting; that is, you can move its start (keeping its duration the same) as long as all meetings remain non-overlapping and within the event’s time window. The relative order of meetings may change after rescheduling. The goal is to maximize the longest continuous free time (i.e. a time interval during which no meeting occurs) during the event after moving at most one meeting.
Key Insights
- The event timeline is initially occupied by fixed meetings that leave “free gaps” at the beginning, between meetings, and at the end.
- Without any move, the maximum free time is the largest of these gaps.
- Rescheduling one meeting effectively “removes” it from its current position, thereby merging its two adjacent free intervals (if it is in the middle) into one larger free interval.
- However, we must place the moved meeting into some other free gap. Thus, we can only perform the reschedule if there exists a free gap (from the original schedule) that is large enough to host the meeting (its duration).
- For each meeting, if removed, the potentially merged free time is: • For the first meeting: from t = 0 to start time of the second meeting. • For the last meeting: from the end time of the second-to-last meeting to eventTime. • For an interior meeting: from the end time of its previous meeting to the start time of its next meeting.
- Finally, the answer is the maximum free time we can achieve either by not moving any meeting or by removing one meeting (subject to the feasibility of re-placing it in an existing free gap).
Space and Time Complexity
Time Complexity: O(n), where n is the number of meetings, because we compute free gaps and check each meeting in one pass. Space Complexity: O(1) (aside from input storage) since we only use a fixed number of extra variables.
Solution
The solution works in two parts:
-
Precompute the maximum free gap available from the original schedule. The free gaps are:
- From time 0 to the start of the first meeting.
- Between consecutive meetings (the gap from the end of meeting j to the start of meeting j+1).
- From the end of the last meeting to eventTime. This maximum gap represents the largest segment where a meeting can be relocated without overlap.
-
For each meeting, consider “removing” it from its current position. The gap that forms by removal is:
- If it is the first meeting: free time = start time of the second meeting − 0.
- If it is the last meeting: free time = eventTime − end time of the second-to-last meeting.
- Otherwise: free time = (start time of the next meeting) − (end time of the previous meeting). This gap is only valid if there is an available free gap (from step 1) that can accommodate the meeting’s duration (i.e. the meeting can be relocated). The meeting’s duration is (endTime[i] − startTime[i]).
Finally, the final answer is the maximum value between the original maximum free gap and all valid gaps obtained by “removing” each possible meeting.
Code Solutions
Below are code implementations with line-by-line comments in Python, JavaScript, C++, and Java.