Skip to content

Most Sales in K Days

01 · Question

Fixed-Length Window

Given an array of sales, find the most sales in any k-day period.

Return 0 when k <= 0 or k > len(sales).

02 · Analysis

Initialize from the first complete window, then add the entering day's sales and subtract the leaving day's sales. This maintains the exact current sum in constant time per shift. The initial maximum must be a real window, so negative totals also work. Return 0 if k <= 0 or k exceeds the input length. Time O(n), auxiliary space O(1).

03 · Solution

Reference solution

1def max_sales(sales: List[int], k: int) -> int:
2 if k <= 0 or k > len(sales):
3 return 0
4 cur = sum(sales[:k])
5 res = cur
6
7 for r in range(k, len(sales)):
8 cur += sales[r]
9 cur -= sales[r - k]
10 res = max(res, cur)
11
12 return res