KatInterview

Minimum Breaks to Cover All Shifts with Rest Windows

Greedy & IntervalsAuto-graded · 17 test cases
MediumAuto-gradedGreedy & Intervalsgreedyintervalsscheduling

Last 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 points s and e.
  • 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 x affects every shift whose open interval contains x (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] with 0 ≤ 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.