Best streak
easy
New here? How this page works
- Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
- Open Break it down. Answer the questions in your own words. This is your plan.
- Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
- Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
- Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
- Press Submit. It runs hidden tests too. Pass them all, and it's solved.
A shop records its profit for each day in profits. A bad day has a negative profit.
Return the highest total profit of any k days in a row.
Examples
Example 1
- Input
- profits = [4, 2, 7, 1, 8, 3], k = 3
- Output
- 16
The 3-day totals are 13, 10, 16 and 12. The best is 7 + 1 + 8 = 16.
Example 2
- Input
- profits = [-1, -2, -3], k = 2
- Output
- -3
Every stretch loses money. The least bad is -1 + -2 = -3.
Example 3
- Input
- profits = [5], k = 1
- Output
- 5
Rules
profitshas between 1 and 100,000 numbers.kis at least 1 and at most the number of days.
Answer each question in your own words before you look at ours. Rough notes are fine: the point is to think before you type code. Your answers stay in this browser.
1.What goes in?
Name every input and what it looks like. Can it be empty? Sorted? Negative?
Stuck? Each hint gives a little more away. Try the problem again after each one.
This explains the whole solution.
You will learn the most by trying first, even if you don’t finish. But if this pattern is new to you, reading the walkthrough first is a fine way to learn it.
1. Understand the problem
We look at every stretch of exactly k days in a row, add up each one, and return the biggest total. Profits can be negative, so the best total can be negative too.
2. Solve an example by hand
[4, 2, 7, 1, 8, 3] with k = 3. The stretches are:
| Days | Total |
|---|---|
| 4, 2, 7 | 13 |
| 2, 7, 1 | 10 |
| 7, 1, 8 | 16 |
| 1, 8, 3 | 12 |
The best is 16. Now look at how each row relates to the one above: from 4, 2, 7 to 2, 7, 1, the 2, 7 stay. Only the 4 leaves and the 1 joins.
3. The slow way
For every starting day, add up k days:
function bestStreak(profits, k) { let best = -Infinity; for (let start = 0; start + k <= profits.length; start++) { let total = 0; for (let i = start; i < start + k; i++) total += profits[i]; best = Math.max(best, total); } return best;}That's about n × k additions. With 100,000 days and k = 50,000, it's 2.5 billion additions. Too slow.
4. Spot the pattern
A stretch of items next to each other, of a fixed size: a sliding window. Neighbouring windows share all but two days, so update the total instead of recounting it.
5. The sliding idea
- Add up the first
kdays. That's the first window, and the best so far. - Slide the window one day at a time: add the day coming in, subtract the day leaving. Keep the best total.
Slide the window one day at a time
profits
- k =
- 3
- total =
- 13
- best =
- 13
function bestStreak(profits, k) { let total = 0; for (let i = 0; i < k; i++) { total += profits[i]; } let best = total; for (let right = k; right < profits.length; right++) { total += profits[right]; // the new day joins total -= profits[right - k]; // the oldest day leaves best = Math.max(best, total); } return best;}Add up the first 3 days: 4 + 2 + 7 = 13. That's the best so far.
6. The code, line by line
let total = 0; for (let i = 0; i < k; i++) { total += profits[i]; } let best = total;Build the first window: days
0tok - 1. Startbestat its total, not at 0: if every day loses money, 0 would be wrong.for (let right = k; right < profits.length; right++) {rightis the day joining the window. It starts at dayk, just after the first window.total += profits[right]; // the new day joins total -= profits[right - k]; // the oldest day leavesThe window always holds
kdays, so when dayrightjoins, dayright - kleaves.best = Math.max(best, total);Keep the best total seen.
7. How fast is it?
| Recount each window | Sliding window | |
|---|---|---|
| Time | O(n × k) | O(n): two small changes per day |
| Extra memory | O(1) | O(1) |
Common mistakes
- Starting
bestat 0. With[-1, -2, -3], no window reaches 0, so the answer must come from a real window. - Subtracting the wrong day. When
rightjoins, the leaving day isright - k. Check with a tiny example:k = 1means the day leaving is the one just before.
Memorise the solution, once you understand it.
This shows the full solution, then helps you rebuild it from memory in four steps. Remembering it is what lets you write it again in an interview, or in a week. Your result goes into your reviews, so it comes back just before you'd forget.
Learn it by heart: Best streak
Read the 13 lines slowly. For each one, say out loud what it does and why it is there. Then cover it and move on to step 2.
function bestStreak(profits, k) { let total = 0; for (let i = 0; i < k; i++) { total += profits[i]; } let best = total; for (let right = k; right < profits.length; right++) { total += profits[right]; // the new day joins total -= profits[right - k]; // the oldest day leaves best = Math.max(best, total); } return best;}