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 nnon-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 longestcontinuous 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.
There is no time during the event not occupied by meetings.
Constraints:
1 <= eventTime <= 109
n == startTime.length == endTime.length
2 <= n <= 105
0 <= startTime[i] < endTime[i] <= eventTime
endTime[i] <= startTime[i + 1] where i lies in the range [0, n - 2].
Solutions
Solution 1: Greedy
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 = \text{endTime}[i] - \text{startTime}[i]\). Then:
\[
l_i = \begin{cases}
0 & i = 0 \\\\
\text{endTime}[i - 1] & i > 0
\end{cases}
\]
\[
r_i = \begin{cases}
\text{eventTime} & i = n - 1 \\\\
\text{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, \(\text{pre}_{i - 1}\), such that \(\text{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, \(\text{suf}_{i + 1}\), such that \(\text{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, \(\text{pre}\) and \(\text{suf}\), where \(\text{pre}[i]\) represents the maximum free time in the range \([0, i]\), and \(\text{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.