A window is a run of consecutive items. Instead of adding up every window from scratch (O(n × k)), slide it: add the item entering and subtract the item leaving.
Fixed windows: the first window's sum, then total += values[i] - values[i - k] for each step.
Variable windows use two pointers, left and right. Grow the window by moving right; when it breaks a rule (a repeated page, more than k empty days, a total over budget), shrink it by moving left until the rule holds again. Track the best window as you go. Each pointer only moves forward, so the whole thing is O(n).
Typical questions: the busiest 7-day period, the longest session without revisiting a page, the shortest run of days that reaches a sales target. Test the edges: a window longer than the data, all items equal, an empty list.
def best_week(daily, k=7):
if len(daily) < k:
return None
total = best = sum(daily[:k])
for i in range(k, len(daily)):
total += daily[i] - daily[i - k]
best = max(best, total)
return best
best_week([5, 1, 3, 8, 2, 9, 4, 7], k=3)
Out: 20, from 9 + 4 + 7. Each slide adds the day entering the window and drops the day leaving it.