Most Sales in K Days

LeetCode

Fixed-Length Window

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

1def max_sales(sales: List[int], k: int) -> int:
2 cur = sum(sales[:k])
3 res = cur
4
5 for r in range(k, len(sales)):
6 cur += sales[r]
7 cur -= sales[r - k]
8 res = max(res, cur)
9
10 return res
i
3
0
2
1
7
2
5
3
1
4
4
5
cur=0
k=3
res=0
Step 1 / 11
Step 1:
Initialize: calculate sum of first k=3 days. sales=[3, 2, 7, 5, 1, 4]
Pointers: i=0
Focus: default
cur=0k=3res=0