Reschedule Meetings for Maximum Free Time II - Problem

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 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]].

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.

Return the maximum amount of free time possible after rearranging the meetings.

Note: The meetings cannot be rescheduled to a time outside the event and they should remain non-overlapping. In this version, it is valid for the relative ordering of the meetings to change after rescheduling one meeting.

Input & Output

Example 1 — Basic Case
$ Input: eventTime = 10, startTime = [1,4,6], endTime = [3,5,8]
Output: 4
💡 Note: Initially gaps are [0,1]=1, [3,4]=1, [5,6]=1, [8,10]=2. By moving meeting [1,3] to [0,2], we get gaps [2,4]=2, [5,6]=1, [8,10]=2, with max gap = 2. By moving it to position [2,4], we merge gaps to get [0,1]=1, [5,6]=1, [8,10]=2. But moving [6,8] to [2,4] gives gaps [0,1]=1, [4,6]=2, [8,10]=2. Actually, moving [1,3] completely out creates a gap [0,1]+[3,4] and placing it at end gives [0,4]=4.
Example 2 — No Improvement
$ Input: eventTime = 8, startTime = [1,3,5], endTime = [2,4,7]
Output: 2
💡 Note: Current gaps are [0,1]=1, [2,3]=1, [4,5]=1, [7,8]=1. Moving any meeting cannot create a gap larger than 2, so the answer remains 2.
Example 3 — Single Meeting
$ Input: eventTime = 10, startTime = [3], endTime = [7]
Output: 7
💡 Note: With one meeting [3,7], current max gap is max(3, 3) = 3. Moving it to [0,4] gives gap [4,10]=6. Moving it to [6,10] gives gap [0,6]=6. Moving to [3,7] gives original max gap 3. Best is moving to start: gap = 7.

Constraints

  • 1 ≤ n ≤ 100
  • 1 ≤ eventTime ≤ 104
  • 0 ≤ startTime[i] < endTime[i] ≤ eventTime
  • All meetings are initially non-overlapping

Visualization

Tap to expand
INPUTALGORITHMRESULTMeeting ScheduleeventTime = 10M1M2M3startTime: [1,4,6]endTime: [3,5,8]Current Gaps:[0,1] = 1[3,4] = 1[5,6] = 1[8,10] = 2Max Gap = 2Rescheduling Steps1Try moving each meeting2Calculate new gaps3Find maximum gapMove M1 to [0,2]:M1M2M3New Gap [2,4] = 2Better: Move to create [0,4] = 4Optimized ScheduleM2M3Maximum Free Time4Gap [0,4] = 4 unitsOptimal reschedulingachieved!Key Insight:The maximum free time is achieved by strategically moving one meeting to createthe largest possible continuous gap, either by merging existing gaps or repositioning optimally.TutorialsPoint - Reschedule Meetings for Maximum Free Time II | Greedy Optimization
Asked in
Google 15 Microsoft 12 Amazon 10 Meta 8
2.5K Views
Medium Frequency
~25 min Avg. Time
89 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen