Highest-Yield Consecutive Stretch in a Profit Log
Dynamic ProgrammingAuto-graded · 20 test casesLast updated
Find the largest total achievable by a non-empty run of adjacent integers, handling all-negative inputs and large sums, in one pass with constant extra space.
Best Consecutive Stretch
A analytics team records the net change of a metric for each day in a list of integers called daily_changes. They want to know the strongest possible result from looking at one unbroken run of days.
Write a function that returns the largest total obtainable by adding up the values in a non-empty block of adjacent entries of the list.
Details
- A block is a sequence of consecutive positions
i, i+1, ..., jwithi <= j. - The block must contain at least one element, so an empty block (total
0) is never a valid answer. - If every value is negative, the answer is the single largest (least negative) element.
- If the list has one element, the answer is that element.
- Totals can be far larger than a 32-bit integer; Python integers handle this natively, but your logic should not assume small sums.
Aim for a solution that runs in linear time and uses only constant extra memory.
Input
daily_changes: a list of integers.
Output
- An integer: the maximum sum over all non-empty contiguous blocks.
Constraints
1 <= len(daily_changes) <= 10^5-10^9 <= daily_changes[i] <= 10^9
Examples
Example 1
Input: best_consecutive_stretch([-2, 1, -3, 4, -1, 2, 1, -5, 4])
Output: 6
Example 2
Input: best_consecutive_stretch([-5, -2, -8, -1, -4])
Output: -1
Hints
Hint 1
For each position, consider only the best block that ends exactly there. How can that be built from the best block ending one position earlier?
Hint 2
At each element you either extend the previous block or begin a new block at this element. When is starting fresh the better option?
Hint 3
Initialize your running values from the first element instead of 0, so that an all-negative list does not wrongly return 0.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Sweep through the list once while maintaining two numbers:
current: the largest sum of a block that ends exactly at the present index.best: the largestcurrentseen so far.
At index i, a block ending there is either just daily_changes[i] by itself, or the best block ending at i - 1 with this element appended. So:
current = max(value, current + value)
After each update, best = max(best, current).
Why it is correct
By induction, after processing index i, current equals the maximum sum among all blocks ending at i. At index 0 the only block is the single element. For later indices, every block ending at i is either the lone element or an extension of some block ending at i - 1; the best extension uses the best earlier block, which is current from the previous step. Every block ends somewhere, so the maximum of all current values (best) is the global answer.
Seeding both variables with the first element (not 0) guarantees only non-empty blocks are considered, so an all-negative list returns its largest element.
Complexity
- Time: O(n), one pass with constant work per element.
- Space: O(1) extra, just two scalars (iterating by index avoids copying the list).
Reference solution
from typing import List
def best_consecutive_stretch(daily_changes: List[int]) -> int:
current = best = daily_changes[0]
for i in range(1, len(daily_changes)):
value = daily_changes[i]
current = max(value, current + value)
best = max(best, current)
return best