Drill

ProblemsGo › scheduling

Extra pay for overnight hours

hardschedulingIntervalsMathGo

Hours between 22:00 and 06:00 carry a premium: each premium hour earns premiumPerHour on top of the base. Compute the added pay for one shift.

nightPremium(start: int, finish: int, premiumPerHour: 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 nightPremium(start int, finish int, premiumPerHour int) int {
	
}

Worked examples

CallResult
nightPremium(1320, 1380, 3000)3000
nightPremium(360, 420, 1000)0
nightPremium(1260, 1440, 1000)2000
nightPremium(0, 120, 1000)2000

Hint

Split the shift against the two half-open bands; a crossing shift spans [start, 1440) plus [0, finish).

Reference solution in Go
func nightPremium(start int, finish int, premiumPerHour int) int {
	if start == finish {
		return 0
	}
	mins := func(x, y int) int {
		if x < y {
			return x
		}
		return y
	}
	maxs := func(x, y int) int {
		if x > y {
			return x
		}
		return y
	}
	overlap := func(a, b, lo, hi int) int {
		v := mins(b, hi) - maxs(a, lo)
		if v < 0 {
			v = 0
		}
		return v
	}
	var minutes int
	if start < finish {
		minutes = overlap(start, finish, 0, 360) + overlap(start, finish, 1320, 1440)
	} else {
		minutes = overlap(start, 1440, 1320, 1440) + overlap(0, finish, 0, 360)
	}
	return minutes * premiumPerHour / 60
}

The same problem in another language

More scheduling problems in Go