Drill

ProblemsJava › scheduling

Rest-period violations in the hand-offs

easyschedulingIntervalsSortingJava

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.

restViolations(shifts: list<Shift>, restMinutes: int) → int

Java 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.

Solve it in Python →

Where you start

int restViolations(List<Shift> shifts, int restMinutes) {
    
}

Worked examples

CallResult
restViolations(Main.<Shift>ls(new Shift(480, 900), new Shift(960, 1200)), 120)1
restViolations(Main.<Shift>ls(new Shift(480, 900), new Shift(1020, 1200)), 120)0
restViolations(Main.<Shift>ls(new Shift(0, 100)), 100)0
restViolations(Main.<Shift>ls(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 Java
int restViolations(List<Shift> shifts, int restMinutes) {
    int bad = 0;
    for (int i = 1; i < shifts.size(); i++) {
        Shift prev = shifts.get(i - 1), cur = shifts.get(i);
        if (cur.start - prev.end < restMinutes) bad++;
    }
    return bad;
}

The same problem in another language

More scheduling problems in Java