A runner's workout is recorded as a list of integers distances, where distances[i] is the number of meters covered during the i-th consecutive 5-second interval. Interval i covers the time from second 5 * i to second 5 * i + 5.
Find the runner's fastest 1000 meters: the maximum average speed, in meters per second, over any continuous stretch of time during which the runner covered exactly 1000 meters.
A stretch may begin and end part-way through an interval. The original assessment required linear interpolation in that case without spelling out the details; this version fixes them as follows: within an interval the runner moves at a constant speed, so after t seconds of interval i (with 0 <= t <= 5) the runner has covered distances[i] * t / 5 meters of that interval.
Function Signature
def fastest_1000m_speed(distances: list[int]) -> float:
Rules
-
A valid stretch is a contiguous time span
[a, b]
with
0 <= a < b <= 5 * len(distances)
during which the runner covers exactly 1000 meters under the constant-speed-within-an-interval model.
-
The average speed of a stretch is
1000 / (b - a)
meters per second. Intervals with 0 meters that lie inside a stretch still add their time.
-
A stretch may lie entirely inside one interval when that interval alone covers at least 1000 meters.
-
Return the maximum average speed over all valid stretches.
-
The exact optimum is a single real number. A returned value is accepted if its relative error from the exact optimum is at most 1% (0.01), matching the tolerance of the original assessment.
Constraints
-
1 <= len(distances) <= 10^5
-
0 <= distances[i] <= 10^5
-
sum(distances) >= 1000
, so at least one valid stretch always exists.
-
sum(distances)
can reach
10^10
, which exceeds
2^31 - 1
; use 64-bit or arbitrary-precision integers for running totals. All values stay well within
2^53
.
Examples
Example 1
Input: distances = [200, 200, 200, 200, 200]
Output: 40.0
The only stretch covering exactly 1000 meters is the whole 25-second run: 1000 / 25 = 40.
Example 2
Input: distances = [100, 400, 400, 300]
Output: 75.0
Intervals 1 and 2 cover 800 meters in 10 seconds. The remaining 200 meters come from the start of interval 3, where the runner moves at 300 / 5 = 60 meters per second, which takes 10/3 seconds. The stretch lasts 40/3 seconds, so the speed is 1000 / (40/3) = 75. No other stretch is faster.
Example 3
Input: distances = [1500]
Output: 300.0
The single interval is run at 1500 / 5 = 300 meters per second, so any 1000 meters inside it take 10/3 seconds: 1000 / (10/3) = 300.