You are given a list schedule of bus departure times, each an integer number of minutes, and an integer current_time. Determine the elapsed time since the most recent bus departed.
The most recent bus that has already departed is the one with the largest departure time that is less than or equal to current_time. Return the number of minutes that have passed since that departure. If no bus has departed yet, return -1.
Function Signature
def elapsed_time(schedule: list[int], current_time: int) -> int:
Rules
-
schedule
is not guaranteed to be sorted and may contain repeated times.
-
A bus whose departure time equals
current_time
has already departed, so the answer in that case is
0
.
-
If every departure time is greater than
current_time
, return
-1
.
Constraints
-
1 <= len(schedule) <= 10^4
-
0 <= schedule[i] <= 10^6
-
0 <= current_time <= 10^6
Examples
Input: schedule = [10, 20, 30, 40, 50], current_time = 35
Output: 5
The buses at 10, 20 and 30 left at or before minute 35. The most recent of them is 30, so the elapsed time is 35 - 30 = 5.
Input: schedule = [100, 200, 300], current_time = 50
Output: -1
Every departure (100, 200, 300) is after minute 50, so no bus has departed yet.
Input: schedule = [45, 5, 30, 30], current_time = 30
Output: 0
The departures at or before minute 30 are 5, 30 and 30. The latest is 30, which is exactly the current time, so 0 minutes have passed.