A bus stop publishes the departure time of every bus for one day. Given that schedule and the current time, return how many minutes you have to wait until the next bus departs.
Function Signature
def minutes_until_next_bus(schedule: list[str], current_time: str) -> int:
Rules
-
Every time, in
schedule
and in
current_time
, is a 24-hour clock string
HH:MM
: a two-digit hour from
00
to
23
, a colon, and a two-digit minute from
00
to
59
.
-
A bus that departs exactly at
current_time
can still be caught, so the wait is
0
.
-
The schedule covers a single day and does not repeat. Only departures at or after
current_time
on the same day count; if there are none, return
-1
.
-
schedule
may be in any order and may list the same time more than once.
-
Return the wait as a whole number of minutes.
Constraints
-
1 <= len(schedule) <= 10^4
-
Every time lies between
00:00
and
23:59
inclusive.
-
The answer is
-1
or an integer from
0
to
1439
.
Examples
Example 1
Input: schedule = ["08:00", "12:30", "17:45"], current_time = "11:50"
Output: 40
The next departure is 12:30, which is 40 minutes after 11:50.
Example 2
Input: schedule = ["09:15", "06:40", "06:40"], current_time = "06:40"
Output: 0
A bus leaves at exactly the current time.
Example 3
Input: schedule = ["07:00", "21:30"], current_time = "22:00"
Output: -1
Every bus has already left for the day.