Best k-Day Total
A shop records its profit for each day of a year in the list profits; a profit can be negative. The owner wants to close for a holiday, and asks which k days in a row brought in the most, so that is not when to close.
The year wraps around: after its last day comes its first day again, so a stretch of days may run from the end of the list into its start. Return the largest total profit of any k days in a row.
Examples
profits = [5, -2, 1, 0, 7], k = 2 → 12
profits = [1, 2, 3, 4], k = 2 → 7
profits = [-3, -1, -2], k = 1 → -1
profits = [4, -9, 3, 6], k = 4 → 4In the first example the best two days are the last one and the first one: 7 + 5 = 12. When k is the whole year, as in the last example, the only stretch is every day once.
Constraints
- Between 1 and 20,000 days, each with a profit between -10,000 and 10,000.
kis at least 1 and at most the number of days.- Aim for O(n): adding up every stretch from scratch costs n × k.
Console
›Run to see what your code prints
Run checks the 4 visible cases here, in your browser. Submit runs all 9 on the judge, 5 of them hidden, and measures how your solution scales.
Expected is the reference solution's answer, from the judge. An input that breaks the statement's other rules may get any answer.