Skip to the code
cf.
+10 XP

#60 Best Streak of Daily Gains

easy

A growth dashboard lists net follower changes per day in gains (they can be negative). It highlights the best streak: the run of back-to-back days (at least one) with the largest total.

Write bestStreak(gains) that returns that largest total.

Example 1

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

Days 2 to 4 give 3 - 1 + 4 = 6.

Example 2

Input: gains = [-4,-2,-7]
Output: -2

Every day is a loss, so the best streak is the single least bad day.

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).