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.
Input: eventTime =5, startTime =[1,3], endTime =[2,5]Output: 2Explanation:
Reschedule the meeting at `[1, 2]` to `[2, 3]`, leaving no meetings during the
time `[0, 2]`.
Input: eventTime =10, startTime =[0,7,9], endTime =[1,8,10]Output: 7Explanation:
Reschedule the meeting at `[0, 1]` to `[8, 9]`, leaving no meetings during the
time `[0, 7]`.
Input: eventTime =10, startTime =[0,3,7,9], endTime =[1,4,8,10]Output: 6Explanation:
Reschedule the meeting at `[3, 4]` to `[8, 9]`, leaving no meetings during the
time `[1, 7]`.
To maximize the longest free time, we can try moving each meeting to every possible valid position (without overlap and within event bounds), and for each configuration, calculate the maximum free gap. Since the number of meetings is small, this brute-force approach is feasible.
classSolution:
defmaxFreeTime(self, eventTime: int, s: list[int], e: list[int]) -> int:
n = len(s)
mt = [(s[i], e[i]) for i in range(n)]
defgetMaxGap(arr: list[tuple[int,int]]) -> int:
arr = sorted(arr)
mx = arr[0][0]
for i in range(1, len(arr)):
mx = max(mx, arr[i][0] - arr[i-1][1])
mx = max(mx, eventTime - arr[-1][1])
return mx
ans = getMaxGap(mt)
for i in range(n):
dur = e[i] - s[i]
rest = [mt[j] for j in range(n) if j != i]
for st in range(eventTime - dur +1):
arr = rest + [(st, st+dur)]
arr.sort()
ok = all(arr[k][0] >= arr[k-1][1] for k in range(1, n))
if ok:
ans = max(ans, getMaxGap(arr))
return ans
impl Solution {
pubfnmax_free_time(event_time: i32, s: Vec<i32>, e: Vec<i32>) -> i32 {
let n = s.len();
letmut mt: Vec<(i32,i32)>= (0..n).map(|i| (s[i],e[i])).collect();
let get_max_gap =|arr: &mut Vec<(i32,i32)>| -> i32 {
arr.sort();
letmut mx = arr[0].0;
for i in1..arr.len() {
mx = mx.max(arr[i].0- arr[i-1].1);
}
mx = mx.max(event_time - arr[arr.len()-1].1);
mx
};
letmut ans = get_max_gap(&mut mt.clone());
for i in0..n {
let dur = e[i] - s[i];
letmut rest: Vec<(i32,i32)>= (0..n).filter(|&j| j!=i).map(|j| (s[j],e[j])).collect();
for st in0..=event_time-dur {
letmut arr = rest.clone();
arr.push((st,st+dur));
arr.sort();
letmut ok =true;
for k in1..arr.len() {
if arr[k].0< arr[k-1].1 { ok =false; break; }
}
if ok {
let g = get_max_gap(&mut arr);
if g > ans { ans = g; }
}
}
}
ans
}
}
⏰ Time complexity: O(n^3 * T) where n is the number of meetings and T is eventTime. For each meeting, we try all possible new positions and check validity and compute gaps.
🧺 Space complexity: O(n) for storing meeting intervals and temporary arrays.
Instead of brute-forcing all possible positions, we can analyze the structure of the free intervals between meetings. For each meeting, there are two main ways to move it to maximize the free time:
If a meeting can be shifted into a non-adjacent free slot (not touching its immediate neighbors), it can create a new free interval equal to the sum of its own duration and the two adjacent free intervals.
Otherwise, moving the meeting merges the two adjacent free intervals into one, minus the duration of the meeting itself.
By precomputing which meetings can be shifted into such slots, we can efficiently determine the maximum possible free interval after moving any meeting.
For each meeting, check if it can be moved into a non-adjacent free slot on the left or right by traversing from both directions and recording the maximum non-adjacent free slot seen so far.
For each meeting, calculate the left and right boundaries (end of previous meeting and start of next meeting).
If the meeting can be moved into a non-adjacent slot, the new free interval is right - left.
Otherwise, the new free interval is right - left - (endTime[i] - startTime[i]).
classSolution {
publicintmaxFreeTime(int eventTime, int[] s, int[] e) {
int n = s.length;
boolean[] q =newboolean[n];
for (int i = 0, t1 = 0, t2 = 0; i < n; i++) {
if (e[i]- s[i]<= t1) q[i]=true;
t1 = Math.max(t1, s[i]- (i == 0 ? 0 : e[i - 1]));
if (e[n - i - 1]- s[n - i - 1]<= t2) q[n - i - 1]=true;
t2 = Math.max(t2, (i == 0 ? eventTime : s[n - i]) - e[n - i - 1]);
}
int ans = 0;
for (int i = 0; i < n; i++) {
int left = i == 0 ? 0 : e[i - 1];
int right = i == n - 1 ? eventTime : s[i + 1];
if (q[i]) ans = Math.max(ans, right - left);
else ans = Math.max(ans, right - left - (e[i]- s[i]));
}
return ans;
}
}
classSolution {
funmaxFreeTime(eventTime: Int, s: IntArray, e: IntArray): Int {
val n = s.size
val q = BooleanArray(n)
var t1 = 0; var t2 = 0for (i in0 until n) {
if (e[i] - s[i] <= t1) q[i] = true t1 = maxOf(t1, s[i] - if (i ==0) 0else e[i-1])
if (e[n-i-1] - s[n-i-1] <= t2) q[n-i-1] = true t2 = maxOf(t2, if (i ==0) eventTime else s[n-i] - e[n-i-1])
}
var ans = 0for (i in0 until n) {
val left = if (i ==0) 0else e[i-1]
val right = if (i == n-1) eventTime else s[i+1]
if (q[i]) ans = maxOf(ans, right-left)
else ans = maxOf(ans, right-left-(e[i]-s[i]))
}
return ans
}
}
classSolution:
defmaxFreeTime(self, eventTime: int, s: list[int], e: list[int]) -> int:
n = len(s)
q = [False]*n
t1 = t2 =0for i in range(n):
if e[i]-s[i] <= t1:
q[i] =True t1 = max(t1, s[i] - (0if i==0else e[i-1]))
if e[n-i-1]-s[n-i-1] <= t2:
q[n-i-1] =True t2 = max(t2, (eventTime if i==0else s[n-i]) - e[n-i-1])
ans =0for i in range(n):
left =0if i==0else e[i-1]
right = eventTime if i==n-1else s[i+1]
if q[i]:
ans = max(ans, right-left)
else:
ans = max(ans, right-left-(e[i]-s[i]))
return ans