Reschedule Meetings for Maximum Free Time I - Problem

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 n non-overlapping meetings, where the ith meeting occurs during the time [startTime[i], endTime[i]].

You can reschedule at most k meetings by moving their start time while maintaining the same duration, to maximize the longest continuous 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.

Input & Output

Example 1 — Basic Rescheduling
$ Input: eventTime = 100, startTime = [10,30,60], endTime = [20,40,80], k = 1
Output: 50
💡 Note: Original gaps are [10,10,20]. By moving the meeting [60,80] to [0,20], we create gaps [20,10,20] with maximum 20. Better: move [10,20] to [80,90] creating gaps [30,20,10] with maximum 30.
Example 2 — No Rescheduling Needed
$ Input: eventTime = 50, startTime = [10], endTime = [20], k = 2
Output: 30
💡 Note: Only one meeting exists. The maximum gap is either before (10 units) or after (30 units) the meeting. Since k=2 > number of meetings, we can move it optimally to get gap of 30.
Example 3 — Multiple Meetings
$ Input: eventTime = 200, startTime = [20,60,120], endTime = [40,80,160], k = 2
Output: 100
💡 Note: Move first two meetings to create large continuous gap. Moving [20,40] and [60,80] to positions [0,20] and [180,200] creates a gap of 100 units from 20 to 120.

Constraints

  • 1 ≤ eventTime ≤ 106
  • 1 ≤ n ≤ 103
  • 0 ≤ startTime[i] < endTime[i] ≤ eventTime
  • 0 ≤ k ≤ n

Visualization

Tap to expand
INPUTALGORITHMRESULTOriginal ScheduleeventTime=100, k=10100M1M2M3Gaps: [10,20,20]Max Gap: 20Greedy Selection1Calculate BenefitsM1: +10, M2: +15, M3: +252Rank by BenefitBest: M3 (+25 units)3Select k=1 MeetingChoose: M34Optimal PlacementMove M3 to beginningOptimized Schedule0100M3M1M2FREE: 30New Gaps: [0,10,10,30]Max Gap: 30Key Insight:Greedy algorithm identifies meetings with highest rescheduling benefitand optimally places them to create maximum continuous free time.TutorialsPoint - Reschedule Meetings for Maximum Free Time I | Greedy Optimization
Asked in
Google 42 Microsoft 38 Amazon 35 Meta 28
27.1K Views
Medium Frequency
~25 min Avg. Time
847 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