Quickest Way to Cover a Set Distance From Per-Interval Odometer Data
Arrays & HashingAuto-graded · 21 test casesLast updated
Find the top average speed over any time window covering exactly a target distance, given piecewise-constant speeds. Tests prefix sums, interpolation and binary search.
A survey drone logs how far it travels in equal time slots. Entry slots[i] is the number of meters flown during slot i, and every slot lasts slot_seconds seconds. Slot i therefore occupies the time interval [i * slot_seconds, (i + 1) * slot_seconds].
Within a single slot the drone moves at a constant speed. Once t seconds of slot i have passed, it has flown slots[i] * t / slot_seconds meters of that slot's distance.
You want the drone's fastest window for a fixed distance. Choose a continuous time window [a, b] in which the drone flies exactly goal meters in total. The window's average speed is goal / (b - a). Return the largest average speed achievable by any such window.
Rules
aandbmay be any real numbers with0 <= a < b <= len(slots) * slot_seconds. A window may begin or end in the middle of a slot.- Slots with
0meters (the drone hovers) still contribute their full duration to any window that contains them. - A window may lie entirely inside one slot, provided that slot alone covers at least
goalmeters. - Results within
1e-6relative error are accepted.
Constraints
1 <= len(slots) <= 10^50 <= slots[i] <= 10^51 <= goal <= sum(slots)slot_secondsis a positive number.- Cumulative distances can exceed 2^31, so use 64-bit integers in fixed-width languages.
- Aim for O(n log n) or better.
Examples
Example 1
Input: fastest_window_speed([100, 100, 100, 100, 100], 10, 500)
Output: 10
Example 2
Input: fastest_window_speed([50, 300, 0, 250, 150], 10, 500)
Output: 17.857142857142858
Example 3
Input: fastest_window_speed([20, 900, 40], 10, 500)
Output: 90
Hints
Hint 1
Build prefix sums of the distances. The total distance flown by time t is a non-decreasing piecewise-linear function, and the best window is the one with the shortest duration.
Hint 2
For a fixed start time, the duration of the best window changes linearly while neither endpoint crosses a slot boundary. So an optimal window can be found with its start or its end exactly on a slot boundary.
Hint 3
For each boundary, binary search the prefix sums for the matching other end and interpolate inside the slot. Use the earliest time for the end and the latest time for the start, so hovering slots are never counted needlessly.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Let P[i] be the total meters flown before slot i (P[0] = 0). The distance covered by time t, call it D(t), is continuous, non-decreasing and piecewise linear. A window [a, b] is valid when D(b) - D(a) = goal. Since the covered distance is fixed, maximizing average speed is the same as minimizing the duration b - a.
Why only boundary candidates matter
Fix a start time a and let b(a) be the earliest valid end. While neither a nor b(a) crosses a slot boundary, both lie on linear pieces of D, so b(a) moves linearly with a. The duration b(a) - a is therefore linear on each such piece, and a linear function on a closed interval attains its minimum at an endpoint. Those endpoints are moments when the start or the end sits exactly on a slot boundary. So it is enough to test:
- Every window whose start is on a boundary: the target cumulative distance is
P[i] + goal. Find the end by binary search over the prefix sums and interpolate inside that slot. - Every window whose end is on a boundary: the target cumulative distance is
P[j] - goal. Find the start the same way.
Handling hovering slots
Zero-distance slots make D flat, so one distance value corresponds to a range of times. To keep the window as short as possible, the end uses the earliest time reaching a given distance (bisect_left), and the start uses the latest time at a given distance (bisect_right - 1). Windows inside a single fast slot are covered because a start at that slot's boundary ends in the same slot, giving duration goal / speed, which is the same for any start inside it.
Correctness
Every valid window can be shifted (without lengthening it) until an endpoint reaches a boundary, and all such candidates are evaluated with exact integer prefix sums and a single float interpolation each. The minimum duration among them is the true minimum, and the answer is goal / minimum duration.
Complexity
- Time: O(n log n) due to the binary searches (a two-pointer sweep can make it O(n)).
- Space: O(n) for the prefix arrays.
from bisect import bisect_left, bisect_right
from typing import List
def fastest_window_speed(slots: List[int], slot_seconds: float, goal: int) -> float:
dist = [0]
clock = [0.0]
for meters in slots:
dist.append(dist[-1] + meters)
clock.append(clock[-1] + slot_seconds)
total = dist[-1]
def earliest_time(meters):
i = bisect_left(dist, meters)
if dist[i] == meters:
return clock[i]
prev = dist[i - 1]
slot_m = dist[i] - prev
return clock[i - 1] + (meters - prev) * slot_seconds / slot_m
def latest_time(meters):
i = bisect_right(dist, meters) - 1
if dist[i] == meters:
return clock[i]
slot_m = dist[i + 1] - dist[i]
return clock[i] + (meters - dist[i]) * slot_seconds / slot_m
best = float('inf')
for d in dist:
# window starts at a boundary-distance d
if d <= total - goal:
best = min(best, earliest_time(d + goal) - latest_time(d))
# window ends at a boundary-distance d
s = d - goal
if 0 <= s <= total - goal:
best = min(best, earliest_time(d) - latest_time(s))
return goal / best