|
| 1 | +''' |
| 2 | +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. |
| 3 | +
|
| 4 | +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]]. |
| 5 | +
|
| 6 | +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. |
| 7 | +
|
| 8 | +Return the maximum amount of free time possible after rearranging the meetings. |
| 9 | +
|
| 10 | +Note that the meetings can not be rescheduled to a time outside the event and they should remain non-overlapping. |
| 11 | +
|
| 12 | +Note: In this version, it is valid for the relative ordering of the meetings to change after rescheduling one meeting. |
| 13 | +
|
| 14 | + ''' |
| 15 | +class Solution: |
| 16 | + def maxFreeTime(self, eventTime: int, startTime: List[int], endTime: List[int]) -> int: |
| 17 | + size = len(startTime) |
| 18 | + gapsArr,left = [],0 |
| 19 | + for i in range(size): |
| 20 | + gap = startTime[i] - left |
| 21 | + gapsArr.append(gap) |
| 22 | + left = endTime[i] |
| 23 | + gapsArr.append(eventTime - endTime[-1]) |
| 24 | + maxGapPrefix,maxGapSuffix = [0 for i in range(size)],[0 for i in range(size)] |
| 25 | + maxGapPrefix[0] = gapsArr[0] |
| 26 | + maxGapSuffix[size-1] = gapsArr[-1] |
| 27 | + |
| 28 | + # calculate left right max |
| 29 | + for i in range(1,size): |
| 30 | + maxGapPrefix[i]= max(maxGapPrefix[i-1],gapsArr[i]) |
| 31 | + for i in range(size-1,0,-1): |
| 32 | + maxGapSuffix[i-1]= max(maxGapSuffix[i],gapsArr[i]) |
| 33 | + |
| 34 | + ans = 0 |
| 35 | + for i in range(size): |
| 36 | + curr = gapsArr[i]+gapsArr[i+1] |
| 37 | + barSize = endTime[i] - startTime[i] |
| 38 | + isValid = False |
| 39 | + if(i-1 >= 0): |
| 40 | + isValid = isValid or maxGapPrefix[i-1] >= barSize |
| 41 | + if(i+1 < size): |
| 42 | + isValid = isValid or maxGapSuffix[i+1] >= barSize |
| 43 | + if(isValid): |
| 44 | + curr += barSize |
| 45 | + ans = max(ans, curr) |
| 46 | + return ans |
| 47 | + |
| 48 | + |
0 commit comments