Drill

ProblemsTypeScript › scheduling

Rest-period violations in the hand-offs

easyschedulingTypeScript

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

Solve it in the editor →

Where you start

function restViolations(shifts: Shift[], restMinutes: number): number {
  
}

Worked examples

CallResult
restViolations([{"start":480,"end":900},{"start":960,"end":1200}], 120)1
restViolations([{"start":480,"end":900},{"start":1020,"end":1200}], 120)0
restViolations([{"start":0,"end":100}], 100)0
restViolations([{"start":0,"end":300},{"start":400,"end":500},{"start":700,"end":800}], 300)2

Hint

Compare each next start to the previous end.

Reference solution in TypeScript
function restViolations(shifts: Shift[], restMinutes: number): number {
  let bad = 0;
  for (let i = 1; i < shifts.length; i++) {
    if (shifts[i].start - shifts[i - 1].end < restMinutes) bad++;
  }
  return bad;
}

The same problem in another language

More scheduling problems in TypeScript