Peak Pace Over Tightest 1000-Meter Windows in Interval Logs
Arrays & HashingAuto-graded · 26 test casesLast updated
Sliding-window problem: for each end index find the shortest suffix with at least 1000 meters, compute its average speed, and return the maximum across all end indices.
A fitness tracker logs how many meters a person moves in each consecutive 5-second tick. You receive a list segments, where segments[i] is the number of meters moved during tick i. Your task is to compute the highest average speed reached over a "tight kilometer" window, as defined below.
Definitions
- A window is a contiguous block of ticks
[l, r]withl <= r. Its distance issegments[l] + ... + segments[r](meters) and its duration is5 * (r - l + 1)seconds. - For every end tick
r, the tight window forris the shortest window[l, r]whose distance is at least1000. Equivalently, it uses the largest possiblelsuch that the distance is still at least1000. If even[0, r]has distance below1000, tickrhas no tight window. - The speed of a window is its entire distance divided by its duration, in meters per second. If the distance exceeds 1000 meters, the whole amount still counts; do not trim or interpolate inside a tick.
Task
Return the maximum speed over the tight windows of all end ticks r that have one.
If the total of all segments is below 1000, no tight window exists, and you must return -1.0.
Only tight windows are considered. A longer window ending at the same r is never considered, even if its average would be higher.
Answers within an absolute or relative error of 1e-6 are accepted.
Constraints
1 <= len(segments) <= 1000000 <= segments[i] <= 1000, all values are integers.- The sum of all segments is at most
100000000. - When a tight window exists, the answer is greater than
0and at most200.0.
Examples
Example 1
Input: peak_tight_km_speed([300, 400, 300, 200, 500, 400])
Output: 73.33333333333333
Example 2
Input: peak_tight_km_speed([250, 250, 250, 240])
Output: -1
Example 3
Input: peak_tight_km_speed([0, 600, 500, 0, 600])
Output: 110
Hints
Hint 1
For a fixed end r, you want the largest l such that the sum of segments[l..r] is still at least 1000. Only that single window matters for r.
Hint 2
All values are non-negative, so as r grows, the best l for r never moves left. This lets you keep one left pointer that only moves forward.
Hint 3
Maintain the running sum of the current window. For each r, while removing segments[l] still leaves a sum of at least 1000, remove it and advance l. Then compute sum / (5 * (r - l + 1)). Skip r while the prefix sum is still under 1000.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
For each end tick r, only one window counts: the shortest [l, r] with sum at least 1000, which is the one with the largest valid l. We need the best speed among these windows.
Because every segment is non-negative, sums are monotone: moving l right never increases a sum, and moving r right never decreases it. So if L(r) is the largest valid start for r, then L(r+1) >= L(r), since [L(r), r+1] already has sum at least 1000. This means a single left pointer that only moves forward is enough.
Algorithm:
- Walk
rfrom left to right, addingsegments[r]to a running window sum. - If the sum is still below 1000, no tight window exists for this
r; continue. (Herelis still 0, so the sum is the full prefix sum.) - Otherwise, while dropping
segments[l]still leaves a sum of at least 1000, drop it and advancel. This also removes leading zeros. - Compute
sum / (5 * (r - l + 1))and update the maximum. - If nothing was ever computed, the answer remains
-1.0.
Why it is correct
After step 3, the window [l, r] has sum at least 1000 and removing its first element would bring the sum below 1000, so it is exactly the shortest qualifying window ending at r. The scan starts from L(r-1) <= L(r), so it never skips past the right start. When l == r, the check becomes 0 >= 1000, which is false, so l never passes r and there is no division by zero. Once the prefix reaches 1000, every later r has a tight window, because the sum only grows as r advances.
Sums are kept as exact integers, and each speed is a single division, so precision is not a concern.
Complexity
- Time: O(n), since each of
landrmoves at most n times. - Space: O(1) beyond the input.
Reference solution
from typing import List
def peak_tight_km_speed(segments: List[int]) -> float:
best = -1.0
left = 0
window_sum = 0
for right, d in enumerate(segments):
window_sum += d
if window_sum < 1000:
continue
while window_sum - segments[left] >= 1000:
window_sum -= segments[left]
left += 1
speed = window_sum / (5 * (right - left + 1))
if speed > best:
best = speed
return best