Minutes Since Last Bus
EasySorting · Visa
A bus stop publishes a daily schedule departures of times in 24-hour "HH:MM" format. The same schedule repeats every day. The list is not sorted and may contain duplicates.
For each time in queries (also "HH:MM"), find the most recent bus that left strictly before that time and return how many minutes ago it left:
- A bus departing exactly at the query time does not count; it has not left yet.
- If no bus left earlier on the same day, the most recent one is the last departure of the previous day.
- If
departuresis empty, the answer for every query is-1.
Return a list of integers, one per query, in the same order as queries.
Example 1
- Input:
- departures = ["08:15", "12:30", "07:45", "12:30"], queries = ["12:40", "08:15", "07:00"]
- Output:
- [10, 30, 1110]
- Explanation
- At 12:40 the last bus left at 12:30, 10 minutes ago. At 08:15 the 08:15 bus has not left yet, so the answer is the 07:45 bus, 30 minutes ago. At 07:00 no bus has left today, so it is yesterday's 12:30 bus: 11 h 30 min until midnight plus 7 h = 1110 minutes.
Example 2
- Input:
- departures = ["23:50"], queries = ["00:05", "23:50"]
- Output:
- [15, 1440]
- Explanation
- At 00:05 the 23:50 bus from the previous day left 15 minutes ago. At exactly 23:50 today's bus has not left, so the last one left 24 hours (1440 minutes) ago.
Example 3
- Input:
- departures = [], queries = ["10:00"]
- Output:
- [-1]
- Explanation
- There is no schedule at all, so the answer is
-1.
Constraints
0 <= departures.length, queries.length <= 10^5- Every time is a valid 24-hour
"HH:MM"string from"00:00"to"23:59"