Food Truck Best Stretch
You run a food truck and log each day's profit; rainy days and festival fees mean some entries are negative. A magazine wants your best run of exactly k days in a row: the highest total profit of any k consecutive days.
Your task
Write bestStretch(profits, k). Return the largest sum of any k consecutive entries of profits.
The answer can be negative if every stretch of k days lost money.
bestStretch(profits: int[], k: int) → int
Examples
profits = [120, -40, 300, 80, -200, 150] k = 3
380
The 3-day stretches sum to 120 - 40 + 300 = 380, -40 + 300 + 80 = 340, 300 + 80 - 200 = 180 and 80 - 200 + 150 = 30. The best is 380.
profits = [-50, -20, -90, -10] k = 2
-70
Every 2-day stretch lost money: -70, -110, -100. The best is -70; starting your running best at 0 would wrongly give 0.
Constraints
- 1 ≤ k ≤ profits.length ≤ 10,000
- -10,000 ≤ profits[i] ≤ 10,000
Start with brute force
For every starting day, add up the next k profits and keep the largest. That is about n × k additions, roughly 25 million for 10,000 days and k = 5,000, and almost all repeated: neighbouring stretches share k - 1 days.
Hints
0 of 4 shown- Hint 1
In the first example, days 0 to 2 sum to 380. The next stretch is days 1 to 3. Which days do they share?
- Hint 2
Neighbouring stretches share all but one day at each end. Moving one day right drops the oldest day and adds a new one.
- Hint 3
So you never need to add up
knumbers again: update the previous total with one subtraction and one addition. - Hint 4
Sum the first
kdays as your best so far. Then for each next dayi, addprofits[i], subtractprofits[i - k], and keep the largest total.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Picture a frame k days wide on the log. Sliding it right lets one day in and pushes one day out, and every day in between stays, so the total changes by just those two days.
Trace [120, -40, 300, 80, -200, 150] with k = 3:
- First frame, days 0 to 2: 120 - 40 + 300 = 380. The best so far is 380.
- Add day 3 (80), drop day 0 (120): 380 + 80 - 120 = 340.
- Add day 4 (-200), drop day 1 (-40): 340 - 200 + 40 = 180.
- Add day 5 (150), drop day 2 (300): 180 + 150 - 300 = 30. The best stays 380.
Start best at the first frame's total, not 0, so an all-losing log still gives the right answer.
function bestStretch(profits, k) {
let total = 0;
for (let i = 0; i < k; i++) total += profits[i];
let best = total;
for (let i = k; i < profits.length; i++) {
total += profits[i] - profits[i - k]; // add the new day, drop the oldest
best = Math.max(best, total);
}
return best;
}def best_stretch(profits, k):
total = sum(profits[:k])
best = total
for i in range(k, len(profits)):
total += profits[i] - profits[i - k] # add the new day, drop the oldest
best = max(best, total)
return bestRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.