Skip to the code
cf.
+25 XP

#64 Locate the Best Streak

medium

A revenue chart should shade the stretch of back-to-back days with the largest total change in gains.

Write bestStreakRange(gains) that returns [start, end], the first and last index of that stretch (at least one day). If several stretches share the largest total, return the one that starts earliest; if they also start on the same day, return the shortest.

Example 1

Input: gains = [2,-5,3,-1,4,-6,2]
Output: [2,4]

3 - 1 + 4 = 6 is the largest total.

Example 2

Input: gains = [1,-1,1]
Output: [0,0]

[0, 0], [2, 2], and [0, 2] all total 1. The earliest start is 0, and the shortest from there is [0, 0].

Constraints

  • 1 <= gains.length <= 10^5
  • -10^4 <= gains[i] <= 10^4
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).