#40 · Sliding window · Beginner · 40 XP · about 8 min
A shop records its takings each day, and sales[i] is the amount for day i. A day can lose money, so amounts can be negative. Write best_k_days(sales, k) that returns the largest total the shop made over any k days in a row.
best_k_days([4, 2, 10, 1, 3, 9], 2) # returns 12 best_k_days([4, 2, 10, 1, 3, 9], 3) # returns 16 best_k_days([-3, -1, -2], 2) # returns -3
For three days in a row, the first three days make 4 + 2 + 10 = 16, more than any other stretch of three.
1 <= k <= len(sales) <= 100,000-10^4 <= sales[i] <= 10^4Ready?
Accept the challenge to start writing. A small timer counts the time this page is in view.
Output appears here.