Minimum Breaks to Cover All Shifts with Rest Windows
Greedy & IntervalsAuto-graded · 17 test casesLast updated
Tests whether a candidate can apply an exchange-argument greedy to schedule the fewest rest-window insertions so every worker shift is covered without violating a maximum continuous-work limit.
Minimum Rest Breaks to Comply with a Work-Hour Limit
A factory operates one production line for a single day. The day is divided into minute-level time slots [0, T). You are given a list of worker shifts: each shift is a half-open interval [start, end) measured in minutes, and shifts may overlap or be contiguous but together they fully cover the whole day (you do not need to worry about gaps between shifts).
The factory has a safety rule: no worker may work more than max_continuous consecutive minutes without taking a break. A break is inserted as a single point in time (it takes no duration itself, but it resets the consecutive-work counter). You must insert the minimum number of breaks such that, within every contiguous stretch of covered time, the gap between any two consecutive break-points (or between a shift boundary and the next break-point, or between the last break-point and a shift boundary) never exceeds max_continuous minutes.
More precisely, for every worker shift [s, e):
- Consider the breakpoints that fall strictly inside
(s, e), plus the two boundary pointssande. - When these points are sorted, the maximum gap between consecutive points must be ≤
max_continuous.
Find the minimum number of break-points to insert (across all shifts) so that every shift satisfies the constraint.
Note: A break-point inserted inside the overlap of two shifts counts as one break and simultaneously satisfies both shifts' constraints at that point. Breaks may be placed at any real-valued minute, but since all inputs are integers and the optimal solution can always use integer positions, you should count only integer break-point positions. A break at position
xaffects every shift whose open interval containsx(i.e.s < x < e).
Your task: implement min_breaks(shifts: List[List[int]], max_continuous: int) -> int that returns the minimum number of break-points required.
Constraints
1 ≤ len(shifts) ≤ 2 000- Each shift is given as
[start, end]with0 ≤ start < end ≤ 86 400(minutes in a day). 1 ≤ max_continuous ≤ 86 400- Shifts are non-overlapping and together cover a contiguous block of the day (there are no gaps between consecutive shifts, though they may be given in any order).
- A break placed at a shift boundary point is not counted (boundaries are free reset points).
- The answer fits in a 32-bit signed integer.
Examples
Example 1
Input: min_breaks([[0, 60], [60, 120], [120, 180]], 60)
Output: 0
Example 2
Input: min_breaks([[0, 90], [90, 180]], 60)
Output: 2
Example 3
Input: min_breaks([[0, 30], [30, 100], [100, 160]], 25)
Output: 5
Hints
Hint 1
Sort the shifts by start time. Since there are no gaps between shifts, treat the merged covered range as one long timeline and think about where breaks are needed relative to shift boundaries.
Hint 2
For each shift [s, e], the covered length is e - s. The number of breaks needed inside it in isolation is floor((e - s - 1) / max_continuous). But a break placed exactly at a shift boundary between two adjacent shifts is free — use this to your advantage.
Hint 3
Use an exchange-argument greedy: within each shift, greedily place each break as late as possible (at position current_last_reset + max_continuous). This minimises the number of breaks while maximising 'carry-over' benefit to the next shift. Track where the last reset happened across shift boundaries.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Observations
-
Sort and merge mentally. Since shifts are non-overlapping and contiguous, sorting by start time gives a single timeline. We process shifts left to right.
-
Shift boundaries are free resets. The boundary between two adjacent shifts (e.g., minute 60 between
[0,60)and[60,120)) acts as a natural reset point at zero cost. -
Exchange-argument greedy: place each break as late as possible. Suppose we must place a break before some deadline
d. Placing it atd(as late as possible) is always at least as good as placing it earlier, because:- It satisfies the current constraint.
- It maximises the window before the next break is needed — any earlier placement can only make the next deadline sooner or equal.
This is the classic "rightmost greedy" / exchange argument: swapping an earlier break for the latest-possible break never increases the total count and can only decrease it or keep it the same.
-
Carry-over across shift boundaries. Because
last_resettracks the actual last reset (whether a break or a boundary), when we cross from one shift to the next, if a break was placed late in the previous shift, that benefit carries forward — we don't need an extra break just because we crossed a boundary.
Algorithm
Sort shifts by start time.
last_reset = first shift's start
For each shift [s, e]:
if last_reset < s: last_reset = s # safety; shouldn't happen
while last_reset + max_continuous < e:
last_reset += max_continuous # place break as late as possible
total_breaks += 1
if e > last_reset: last_reset = e # boundary reset
return total_breaks
Why It Is Correct
- Feasibility: after the loop for a shift,
last_reset + max_continuous >= end, so the remaining stretch[last_reset, end]has length≤ max_continuous. Combined with all previously placed breaks, no gap exceeds the limit. - Optimality: by the exchange argument, any solution that places a break earlier than our chosen position can be "slid right" to our position without violating any constraint and without increasing the count. Hence our greedy achieves the minimum.
Complexity
- Time: O(n log n) for sorting + O(T / max_continuous) for the while loop across all shifts (where T is the total covered time). In the worst case T = 86 400 and max_continuous = 1, giving O(86 400) iterations — well within limits.
- Space: O(n) for the sorted copy.
Reference Solution
from typing import List
def min_breaks(shifts: List[List[int]], max_continuous: int) -> int:
shifts_sorted = sorted(shifts, key=lambda x: x[0])
total_breaks = 0
last_reset = shifts_sorted[0][0]
for start, end in shifts_sorted:
if last_reset < start:
last_reset = start
while last_reset + max_continuous < end:
last_reset += max_continuous
total_breaks += 1
if end > last_reset:
last_reset = end
return total_breaks