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.
NightPremium(start: int, finish: int, premiumPerHour: int) → int
C# 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.
Where you start
public int NightPremium(int start, int finish, int premiumPerHour) {
}
Worked examples
| Call | Result |
|---|---|
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 C#
public int NightPremium(int start, int finish, int premiumPerHour) {
if (start == finish) return 0;
int minutes;
if (start < finish) {
minutes = Math.Max(0, Math.Min(finish, 360) - start)
+ Math.Max(0, finish - Math.Max(start, 1320));
} else {
minutes = 1440 - Math.Max(start, 1320)
+ Math.Min(finish, 360);
}
return minutes * premiumPerHour / 60;
}