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.changes=[3, -5, 4, -1, 6, -8, 2] → 9
The best streak covers the days at indices 2 through 4: 4 − 1 + 6 = 9.
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.
changes=[6, -1, 2] → 7
Taking all three days pays off best: 6 − 1 + 2 = 7.
changes=[3, -5, 4, -1, 6, -8, 2]
9
Run checks the first 3 tests in your browser. Submit checks all 101 tests on the server, hidden ones included.