Problems › Python › scheduling
Extra pay for overnight hours
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.
- The premium band is [22:00, 24:00) plus [00:00, 06:00), on the minute.
- A shift is described by start and finish minutes past midnight; a finish earlier than start means the shift crosses midnight.
- Return only the added pay: premium minutes ÷ 60 × premiumPerHour, rounded down.
night_premium(start: int, finish: int, premium_per_hour: int) → int
Where you start
def night_premium(start: int, finish: int, premium_per_hour: int) -> int:
Worked examples
| Call | Result |
|---|---|
night_premium(1320, 1380, 3000) | 3000 |
night_premium(360, 420, 1000) | 0 |
night_premium(1260, 1440, 1000) | 2000 |
night_premium(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 Python
def night_premium(start: int, finish: int, premium_per_hour: int) -> int:
if start == finish:
return 0
def overlap(a, b, lo, hi):
return max(0, min(b, hi) - max(a, lo))
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 * premium_per_hour // 60