Rest-period violations in the hand-offs
Between any two shifts assigned to the same person there must be at least restMinutes of free time. Count the hand-offs in a day that break the rule.
- Each pair of consecutive shifts is one hand-off; consider them in the order listed.
- Sequential per person — here the day is one person's list, so every gap counts.
- Ending at 900 and starting at 1020 is exactly restMinutes if restMinutes is 120, which is fine.
RestViolations(shifts: list<Shift>, restMinutes: int) → int
C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
public int RestViolations(List<Shift> shifts, int restMinutes) {
}
Worked examples
| Call | Result |
|---|---|
RestViolations(new List<Shift> { new Shift(480, 900), new Shift(960, 1200) }, 120) | 1 |
RestViolations(new List<Shift> { new Shift(480, 900), new Shift(1020, 1200) }, 120) | 0 |
RestViolations(new List<Shift> { new Shift(0, 100) }, 100) | 0 |
RestViolations(new List<Shift> { new Shift(0, 300), new Shift(400, 500), new Shift(700, 800) }, 300) | 2 |
Hint
Compare each next start to the previous end.
Reference solution in C#
public int RestViolations(List<Shift> shifts, int restMinutes) {
int bad = 0;
for (int i = 1; i < shifts.Count; i++) {
if (shifts[i].Start - shifts[i - 1].End < restMinutes) bad++;
}
return bad;
}