Ctrl K

Most Profitable Streak

Medium
A shop logged its daily profit in the array changes: changes[i] is the profit on day i, with a loss written as a negative number. Find a run of consecutive days (at least one day long) with the largest total profit and return that total. An O(n²) solution is obvious; try to do it in one pass.
Example 1
changes=[3, -5, 4, -1, 6, -8, 2] → 9

The best streak covers the days at indices 2 through 4: 4 − 1 + 6 = 9.

Example 2
changes=[-4, -2, -7] → -2

Every day is a loss; the streak must be non-empty, so we take the single day with the smallest loss: −2.

Example 3
changes=[6, -1, 2] → 7

Taking all three days pays off best: 6 − 1 + 2 = 7.

Constraints
  • 1 ≤ len(changes) ≤ 10⁵
  • -10⁴ ≤ changes[i] ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBest Run on a Ring MediumOther Orders for the Same Search Tree HardRising Chain With Small Steps HardSpaced Picks After Edits HardFewest Jumps to the End Medium
loading Python…
Input
changes=[3, -5, 4, -1, 6, -8, 2]
Expected
9

Run checks the first 3 tests in your browser. Submit checks all 101 tests on the server, hidden ones included.