Drill

ProblemsPython › scheduling

Rest-period violations in the hand-offs

easyschedulingPython

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.

rest_violations(shifts: list<Shift>, rest_minutes: int) → int

Solve it in the editor →

Where you start

def rest_violations(shifts: list[Shift], rest_minutes: int) -> int:
    

Worked examples

CallResult
rest_violations([Shift(start=480, end=900), Shift(start=960, end=1200)], 120)1
rest_violations([Shift(start=480, end=900), Shift(start=1020, end=1200)], 120)0
rest_violations([Shift(start=0, end=100)], 100)0
rest_violations([Shift(start=0, end=300), Shift(start=400, end=500), Shift(start=700, end=800)], 300)2

Hint

Compare each next start to the previous end.

Reference solution in Python
def rest_violations(shifts: list[Shift], rest_minutes: int) -> int:
    return sum(1 for i in range(1, len(shifts)) if shifts[i].start - shifts[i - 1].end < rest_minutes)

The same problem in another language

More scheduling problems in Python