Shift Duration
Medium · rating 1300 · Implementation, Math
A worker clocks in and out using a 24-hour clock. The first line is the start time and the second is the end time, each formatted HH:MM. Print the length of the shift in minutes. If the end time is not after the start time, the shift ran overnight into the next day.
Editorial
Approach
Convert both times to minutes since midnight (H×60 + M) and subtract. If the end is earlier than the start the shift crossed midnight, so take the difference modulo 1440 (minutes in a day) to wrap it into the next day.
def mins(t):
h, m = map(int, t.split(':'))
return h * 60 + m
print((mins(end) - mins(start)) % 1440)Edge case
Equal start and end times give 0, treated as a zero-length shift rather than a full 24 hours.
Complexity
Time: O(1). Space: O(1).
Related problems
- Credit Card Validation — Medium
- Collatz Steps — Medium
- Overtime Pay — Medium
- Format a Video Length — Medium
- RGB to Hex Color — Easy
- FizzBuzz — Easy