Elapsed Minutes Since the Latest Bus Pull-Out
StringsAuto-graded · 23 test casesLast updated
Convert clock strings to minutes and find the shortest wrap-around gap back to a past departure, covering unsorted input, duplicates and an empty schedule.
Elapsed Minutes Since the Latest Bus Pull-Out
A depot keeps a daily timetable of pull-out times for one route. The timetable repeats every day. Each time is a 24-hour clock string "HH:MM". You are also given the current clock time as a string in the same format.
Write a function that returns how many minutes have passed since the most recent pull-out.
Rules
- If at least one pull-out time is at or before the current time on the clock, the most recent pull-out is the latest such time today. The answer is the current time minus that time, in minutes.
- A pull-out whose time is exactly equal to the current time counts as having just happened, so the answer is
0. - If every pull-out time is later in the day than the current time, nothing has left yet today. The most recent pull-out is then the latest pull-out time of yesterday, and the answer is the number of minutes from that moment, across midnight, to the current time.
- The list may be unsorted and may contain repeated times. Repeats do not change the answer.
- If the list of pull-out times is empty, return
-1.
Input
schedule: a list of strings, each in"HH:MM"format.now: a string in"HH:MM"format.
Output
An integer: the number of minutes since the most recent pull-out, or -1 if schedule is empty.
Constraints
schedulenormally contains between 1 and 1000 strings; an empty list must still return-1.- Every time is a valid
"HH:MM"between00:00and23:59. scheduleis not guaranteed to be sorted.- The answer is always between 0 and 1439 when the list is non-empty.
Examples
Example 1
Input: minutes_since_last_departure(['08:30', '14:00', '18:15'], '15:00')
Output: 60
Example 2
Input: minutes_since_last_departure(['09:00', '22:00'], '06:00')
Output: 480
Example 3
Input: minutes_since_last_departure(['08:00', '08:00', '12:00'], '08:00')
Output: 0
Hints
Hint 1
Convert every time to minutes since midnight so that comparisons and subtraction become plain integer operations.
Hint 2
Split into two cases: some pull-out is at or before the current minute, or none is. In the second case the latest pull-out of the whole schedule happened yesterday.
Hint 3
A day has 1440 minutes. The gap from a pull-out at minute d to the current minute t is (t - d) modulo 1440, and you want the smallest such gap.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Convert each "HH:MM" string to minutes since midnight (hours * 60 + minutes), so every time becomes an integer from 0 to 1439. Let current be the converted now.
There are two cases:
- Some pull-out is at or before
current. Take the largest such value. The answer iscurrent - that_value. An exact match gives0. - Every pull-out is after
current. Nothing has left yet today, so the most recent one is yesterday's latest pull-out, which is the maximum of the whole list. It occurred1440 - max_timeminutes before midnight, andcurrentminutes have passed since midnight, so the answer iscurrent + 1440 - max_time.
If the list is empty, return -1.
Why it is correct
- In case 1, any pull-out later than
currentlast happened yesterday, so it is further in the past than any pull-out from earlier today. Among today's candidates, the largest time gives the smallest gap. - In case 2, all candidates happened yesterday. Pull-out time
dwascurrent + 1440 - dminutes ago, which is smallest whendis largest. - Duplicates do not matter because only the maximum is used.
- Sorting is unnecessary because only maxima are needed.
An equivalent formulation is min((current - d) % 1440 for d in times).
Complexity
- Time: O(n), one pass to parse and one to scan.
- Space: O(n) for the converted list, which could be reduced to O(1) by tracking two running maxima.
Reference solution
from typing import List
def minutes_since_last_departure(schedule: List[str], now: str) -> int:
if not schedule:
return -1
def to_minutes(value: str) -> int:
hours, minutes = value.split(":")
return int(hours) * 60 + int(minutes)
current = to_minutes(now)
times = [to_minutes(v) for v in schedule]
best_today = -1
for t in times:
if t <= current and t > best_today:
best_today = t
if best_today >= 0:
return current - best_today
return current + 1440 - max(times)