Ctrl K

Move One Meeting for the Longest Break

Medium
A workday runs from time 0 to time day_end. It holds n non-overlapping meetings sorted by time: meeting i takes the interval [starts[i], ends[i]]. You may move **at most one** meeting to any other time. The moved meeting keeps its duration, stays within the workday and must not overlap the others. It does not have to keep its place in the order: it can land before or after any other meeting. Return the longest possible stretch of continuous free time within the day.
Example 1
day_end=12, starts=[0, 3, 10], ends=[2, 4, 11] → 8

Move the meeting [3, 4] to the very end of the day, [11, 12]. Then nothing happens from 2 to 10, a break of length 8.

Example 2
day_end=9, starts=[1, 4], ends=[3, 8] → 2

Each free piece is 1 unit long and no meeting fits into a gap elsewhere. All you can do is slide a meeting to merge its two neighbouring gaps: for example, [1, 3] → [0, 2] frees [2, 4].

Example 3
day_end=3, starts=[0, 1], ends=[1, 3] → 0

The meetings fill the whole day, so there is no free time at all.

Constraints
  • 1 ≤ day_end ≤ 10⁹
  • 2 ≤ n = len(starts) = len(ends) ≤ 10⁵
  • 0 ≤ starts[i] < ends[i] ≤ day_end
  • ends[i] ≤ starts[i + 1]
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLevel the Jars MediumStrongest Product Team MediumShift a Segment to Match MediumFlowerbed Beauty on a Budget HardLevel Up in Singles and Pairs Hard
loading Python…
Input
day_end=12, starts=[0, 3, 10], ends=[2, 4, 11]
Expected
8

Run checks the first 3 tests in your browser. Submit checks all 113 tests on the server, hidden ones included.