Max Speed Continuous Window on Ticked Rate Stream
Greedy & IntervalsAuto-graded · 20 test casesLast updated
Find the maximum average throughput over a continuous time window of a specified size, given rate logs over uniform time ticks.
An automated logging system measures production output over consecutive time intervals of uniform duration tick_seconds. The list ticks contains non-negative integers where ticks[i] represents the total count of items produced during the $i$-th tick.
Within any single tick, production is assumed to occur at a constant uniform rate. Thus, if $t$ seconds have elapsed into tick $i$ (where $0 \le t \le \text{tick_seconds}$), exactly $\text{ticks}[i] \times \frac{t}{\text{tick_seconds}}$ items have been processed in that tick.
Your objective is to find a continuous time window during which exactly target_units items were produced such that the average speed (items per second) during that window is maximized.
A valid window can start and end at any fractional second across the total duration. The average speed of a window of duration $D$ seconds is $\frac{\text{target_units}}{D}$. Minimizing the duration $D$ maximizes the average speed.
Constraints
1 <= len(ticks) <= 10^51 <= tick_seconds <= 600 <= ticks[i] <= 10^5sum(ticks) >= target_units1 <= target_units <= 10^9
Examples
Example 1
Input: fastest_stretch_speed([200, 200, 200, 200, 200], 4, 1000)
Output: 50
Example 2
Input: fastest_stretch_speed([33, 46, 4], 2, 52)
Output: 22.000000000000004
Hints
Hint 1
Minimizing the window's total duration for a fixed target output maximizes the average speed.
Hint 2
Continuous rate changes only occur at tick boundaries. Consider how moving a window's start/end points affects duration when the entry rate differs from the exit rate.
Hint 3
An optimal window will always have at least one of its endpoints aligned with a tick boundary, unless the target output is completed entirely within a single tick.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
To maximize the average speed $\frac{\text{target_units}}{D}$, we need to minimize the duration $D$ (in seconds) required to produce target_units.
Optimal Endpoint Property
Suppose a candidate continuous window starts at time $t_1$ and ends at time $t_2$.
- If both $t_1$ and $t_2$ lie strictly inside ticks (not on boundaries), the rate of item output at $t_1$ is $r_{\text{start}}$ and at $t_2$ is $r_{\text{end}}$.
- Shifting the window slightly by $\delta t$ changes the total duration required to cover
target_units. If $r_{\text{start}} \neq r_{\text{end}}$, sliding the window in one direction will strictly decrease the duration. - Therefore, a local minimum duration occurs only when at least one endpoint aligns with a tick boundary or when the window falls entirely within a single tick.
Algorithm Steps
- Compute the prefix sum array of production counts to quickly map cumulative units to timestamps.
- Iterate through every tick boundary $i \in [0, n]$:
- Treat boundary $i$ as the window start: Find the earliest time $t_{\text{end}}$ when cumulative output reaches $\text{prefix}[i] + \text{target_units}$. The duration is $t_{\text{end}} - i \times \text{tick_seconds}$.
- Treat boundary $i$ as the window end: Find the latest time $t_{\text{start}}$ when cumulative output is $\text{prefix}[i] - \text{target_units}$. The duration is $i \times \text{tick_seconds} - t_{\text{start}}$.
- Check all individual ticks that can contain
target_unitsentirely by themselves. - Take the overall minimum duration $D_{\min}$ and return $\frac{\text{target_units}}{D_{\min}}$.
Complexity
- Time Complexity: $\mathcal{O}(N \log N)$ where $N = \text{len(ticks)}$, as we perform binary searches (
bisect_left/bisect_right) over the prefix array of size $N+1$ for each of the $N+1$ boundaries. (This can also be implemented in $\mathcal{O}(N)$ using a two-pointer approach). - Space Complexity: $\mathcal{O}(N)$ to store the prefix sums.
from bisect import bisect_left, bisect_right
from typing import List
def fastest_stretch_speed(ticks: List[int], tick_seconds: int, target_units: int) -> float:
n = len(ticks)
prefix = [0]
for units in ticks:
prefix.append(prefix[-1] + units)
total = prefix[-1]
best_duration = float("inf")
# Earliest timestamp where cumulative output reaches q
def earliest_time(q: int) -> float:
k = bisect_left(prefix, q)
if k < len(prefix) and prefix[k] == q:
return k * tick_seconds
i = k - 1
return (i + (q - prefix[i]) / ticks[i]) * tick_seconds
# Latest timestamp where cumulative output is q
def latest_time(q: int) -> float:
k = bisect_right(prefix, q) - 1
if prefix[k] == q:
return k * tick_seconds
i = k
return (i + (q - prefix[i]) / ticks[i]) * tick_seconds
for i in range(n + 1):
q = prefix[i] + target_units
if q <= total:
best_duration = min(best_duration, earliest_time(q) - i * tick_seconds)
q = prefix[i] - target_units
if q >= 0:
best_duration = min(best_duration, i * tick_seconds - latest_time(q))
for i, units in enumerate(ticks):
if units >= target_units:
best_duration = min(best_duration, tick_seconds * target_units / units)
return target_units / best_duration