You are given a floating-point number hour, the amount of time you have to reach the office. To commute to the office you must take n trains in order. You are also given an integer array dist of length n, where dist[i] is the distance in kilometers of train ride i.
Each train can depart only at an integer hour, so you may need to wait between train rides. You do not wait after the last ride. For example, if the first ride takes 1.5 hours, you wait an extra 0.5 hours and the second ride departs at the 2 hour mark.
Return the minimum positive integer speed, in kilometers per hour, that all the trains must travel at for you to reach the office on time, or -1 if it is impossible to be on time.
Function Signature
def min_speed_on_time(dist: list[int], hour: float) -> int:
Rules
-
All trains travel at the same speed, and ride
i
takes
dist[i] / speed
hours.
-
The first train leaves at hour 0. After every ride except the last, you wait until the next integer hour. A ride that ends exactly on an integer hour needs no wait.
-
You are on time when the total time, waits included, is at most
hour
.
Constraints
-
n == len(dist)
-
1 <= n <= 10^5
-
1 <= dist[i] <= 10^5
-
1 <= hour <= 10^9
-
hour
has at most two digits after the decimal point.
Examples
Input: dist = [1, 3, 2], hour = 6
Output: 1
At speed 1 the rides take 1, 3 and 2 hours, all whole numbers, so there is no waiting and you arrive at exactly hour 6. Arriving exactly at the limit counts as on time, and there is no smaller positive speed.
Input: dist = [1, 3, 2], hour = 2.7
Output: 3
At speed 3 the first ride ends at hour 1/3 and you wait until hour 1, the second ends at hour 2, and the last takes 2/3 of an hour, so you arrive at about hour 2.67. At speed 2 the second ride ends at hour 2.5, you wait until hour 3, and you arrive at hour 4, which is late.
Input: dist = [1, 3, 2], hour = 1.9
Output: -1
Each of the first two rides takes positive time and ends with a wait until an integer hour, so the third train cannot leave before hour 2, which is already past 1.9.