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 nnon-overlapping meetings, where the ith meeting occurs during the time [startTime[i], endTime[i]].
You can reschedule at mostk meetings by moving their start time while maintaining the same duration, to maximize the longestcontinuous 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.
There is no time during the event not occupied by meetings.
Constraints:
1 <= eventTime <= 109
n == startTime.length == endTime.length
2 <= n <= 105
1 <= k <= n
0 <= startTime[i] < endTime[i] <= eventTime
endTime[i] <= startTime[i + 1] where i lies in the range [0, n - 2].
Solutions
Solution 1: Sliding Window
The problem is essentially about merging adjacent free time intervals into a longer free interval. There are \(n + 1\) free intervals in total:
The first free interval is from the start of the event to the start of the first meeting;
The middle \(n - 1\) free intervals are between each pair of adjacent meetings;
The last free interval is from the end of the last meeting to the end of the event.
At most \(k\) meetings can be rescheduled, which is equivalent to merging up to \(k + 1\) free intervals. We need to find the maximum length among all possible merged \(k + 1\) free intervals.
We can store the lengths of these free intervals in an array \(\textit{nums}\). Then, we use a sliding window of length \(k + 1\) to traverse this array, calculate the sum for each window, and find the maximum sum, which is the maximum free time we are looking for.
The time complexity is \(O(n)\), and the space complexity is \(O(n)\), where \(n\) is the number of meetings.
In Solution 1, we used an array to store the lengths of the free intervals. In fact, we do not need to store the entire array; we can use a function \(f(i)\) to represent the length of the \(i\)-th free interval. This way, we can save space.
The time complexity is \(O(n)\), where \(n\) is the number of meetings. The space complexity is \(O(1)\).