Drill

ProblemsGo › scheduling

Rest-period violations in the hand-offs

easyschedulingIntervalsSortingGo

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

Go 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

func restViolations(shifts []Shift, restMinutes int) int {
	
}

Worked examples

CallResult
restViolations([]Shift{Shift{Start: 480, End: 900}, Shift{Start: 960, End: 1200}}, 120)1
restViolations([]Shift{Shift{Start: 480, End: 900}, Shift{Start: 1020, End: 1200}}, 120)0
restViolations([]Shift{Shift{Start: 0, End: 100}}, 100)0
restViolations([]Shift{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 Go
func restViolations(shifts []Shift, restMinutes int) int {
	bad := 0
	for i := 1; i < len(shifts); i++ {
		if shifts[i].Start-shifts[i-1].End < restMinutes {
			bad++
		}
	}
	return bad
}

The same problem in another language

More scheduling problems in Go