Quickest Kilometer Within a Run Recorded in 5-Second Splits
Arrays & HashingAuto-graded · 25 test casesLast updated
Find the highest average speed over any 1000 m stretch of a run given per-5-second distances, using prefix sums, binary search and interpolation at boundaries.
A fitness tracker logs a run as a list of non-negative integers splits. Entry splits[i] is the number of meters covered during the i-th 5-second slot, which spans seconds 5*i to 5*i + 5.
Your task is to find the runner's best kilometer: the highest average speed (in meters per second) achieved over any continuous span of time in which exactly 1000 meters are covered.
Motion model
Within a single slot the runner moves at a constant speed. After t seconds of slot i (where 0 <= t <= 5), the runner has covered splits[i] * t / 5 meters of that slot. A span may therefore start or end part-way through a slot.
Rules
- A valid span is a time range
[a, b]with0 <= a < b <= 5 * len(splits)in which exactly 1000 meters are covered under the model above. - The average speed of a span is
1000 / (b - a). Slots with 0 meters that fall inside a span still contribute their full time tob - a. - A span may lie entirely inside one slot if that slot alone covers at least 1000 meters.
- Return the maximum average speed over all valid spans. This is the same as finding the span with the smallest duration.
- Your answer is accepted if its relative error from the exact optimum is at most 1% (0.01).
Constraints
1 <= len(splits) <= 10^50 <= splits[i] <= 10^5sum(splits) >= 1000, so at least one valid span always exists.sum(splits)can reach10^10, which is larger than a 32-bit integer. Use 64-bit or arbitrary-precision totals.
Examples
Example 1
Input: best_kilometer_speed([200, 200, 200, 200, 200])
Output: 40
Example 2
Input: best_kilometer_speed([100, 400, 400, 300])
Output: 75
Example 3
Input: best_kilometer_speed([1500])
Output: 300
Hints
Hint 1
Switch from thinking in time to thinking in distance. Build prefix sums so you can ask at what time the runner had covered a given total distance.
Hint 2
If you fix the distance at which a span starts, its end distance is that plus 1000, so the duration is a function of the start distance. This function is piecewise linear, and its breakpoints are where the start or the end lands on an interval boundary.
Hint 3
A piecewise linear function reaches its minimum at a breakpoint, so only spans that start on a slot boundary or end on a slot boundary need checking. Binary search the prefix sums to find the slot containing the other end, and interpolate inside it.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Work with distance instead of time. Let pref[k] be the total distance covered at time 5k (the prefix sums, with pref[0] = 0).
A span is determined by its starting distance x, because it must finish at distance x + 1000. Let time_at(d) be the time at which the runner reaches total distance d. Inside a slot with positive distance s, time grows linearly with distance at 5 / s seconds per meter, so time_at is piecewise linear with breakpoints at the prefix sums. The span duration is
f(x) = time_at(x + 1000) - time_at(x)
and f changes slope only when x or x + 1000 passes a prefix sum. Between those points f is linear, so its minimum on any closed piece is at an endpoint. The domain ends (x = 0 and x + 1000 = total) are also of this form. So some optimal span either starts or ends on a slot boundary, which leaves at most 2n candidates.
- Start on boundary
k. The end distance istarget = pref[k] + 1000. Iftarget > total, stop. Otherwise find the firstjwithpref[j] >= targetusing binary search. The end lies in slotj - 1, so interpolate:end = 5*(j-1) + 5*(target - pref[j-1]) / splits[j-1]. - End on boundary
j. The start distance isorigin = pref[j] - 1000. If it is negative, skip. Otherwise find the lastkwithpref[k] <= origin. The start lies in slotk, so interpolate:start = 5*k + 5*(origin - pref[k]) / splits[k].
Take the smallest duration and return 1000 / duration.
Why it is correct
- The candidates cover every breakpoint of
f, so the true minimum duration is among them. - Zero-distance slots. When the runner stands still, the time for a given distance is not unique. The shortest span uses the latest possible start and the earliest possible end.
bisect_leftfinds the earliest momenttargetis reached, sopref[j-1] < targetand the slot used has positive distance, which also avoids division by zero.bisect_right(...) - 1finds the last moment the runner is atorigin, sopref[k] <= origin < pref[k+1]and that slot also has positive distance. Zero slots strictly inside a span still add their time. - A span inside one slot is handled naturally. For
[1500], starting at boundary 0 givestarget = 1000, and the duration is5 * 1000 / 1500 = 10/3, so the speed is 300. - Check on
[100, 400, 400, 300]: starting at time 5 (100 m covered), the end target is 1100 m, which is 200 m into the last slot at 60 m/s. The end is at15 + 10/3 = 55/3, so the duration is55/3 - 5 = 40/3and the speed is 75. The other candidates are slower.
Complexity
- Time:
O(n log n)for2nbinary searches over the prefix array (a two-pointer sweep would giveO(n)). - Space:
O(n)for the prefix sums.
Reference solution
from bisect import bisect_left, bisect_right
from typing import List
def best_kilometer_speed(splits: List[int]) -> float:
n = len(splits)
pref = [0] * (n + 1)
for i, d in enumerate(splits):
pref[i + 1] = pref[i] + d
total = pref[n]
best = None
# Spans that begin exactly on a slot boundary.
for k in range(n):
target = pref[k] + 1000
if target > total:
break
j = bisect_left(pref, target) # first j with pref[j] >= target
d = splits[j - 1]
end_time = 5 * (j - 1) + 5 * (target - pref[j - 1]) / d
dur = end_time - 5 * k
if best is None or dur < best:
best = dur
# Spans that end exactly on a slot boundary.
for j in range(1, n + 1):
origin = pref[j] - 1000
if origin < 0:
continue
k = bisect_right(pref, origin) - 1 # last k with pref[k] <= origin
d = splits[k]
start_time = 5 * k + 5 * (origin - pref[k]) / d
dur = 5 * j - start_time
if best is None or dur < best:
best = dur
return 1000 / best