Problems › JavaScript › scheduling
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
Where you start
function restViolations(shifts, restMinutes) {
}
Worked examples
| Call | Result |
|---|---|
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 JavaScript
function restViolations(shifts, restMinutes) {
let bad = 0;
for (let i = 1; i < shifts.length; i++) {
if (shifts[i].start - shifts[i - 1].end < restMinutes) bad++;
}
return bad;
}