01
02
03
04
05
06
07
08
09
10
11
A
A2Z Sheet

72. Maximum Consecutive Ones & The Winning Streak

Easy
Step 3: Solve Problems on Arrays [Easy -> Medium -> Hard]›Easy

Maximum Consecutive Ones & The Winning Streak

EasyFunction: maximumConsecutiveOnes()
ASCI Mission Breakdown • Simple as Hell
"Find the maximum number of consecutive 1s in a binary array."
Real-World Metaphor:

Think of flipping a coin. You want to see what is your longest consecutive streak of heads before a tail interrupts you.

Interactive Visual WalkthroughARRAY-POINTERS
Step 1 / 3
Matches: [1, 1, 0, 1, 1, 1]
1
[0]
streak
1
[1]
0
[2]
1
[3]
1
[4]
1
[5]
Memory Notepad / State Tracker
Current Streak:2
Max Streak:2
Evaluating

1. First Winning Streak

First two matches are wins: streak = 2, max = 2.

How to Think About This (Mental Model)

  1. Keep two counters: currentStreak = 0, maxStreak = 0.
  2. Iterate through the array:
  3. If num === 1, currentStreak++, maxStreak = Math.max(maxStreak, currentStreak).
  4. If num === 0, currentStreak resets to 0.
  5. Return maxStreak.

### The Mission Imagine tracking a game where `1` represents a win and `0` represents a loss in an array of match results: `[1, 1, 0, 1, 1, 1]`. Your mission is to calculate the **longest uninterrupted winning streak** (maximum consecutive 1s) achieved throughout the season.

Examples

Example 1
Input: nums = [1,2,3,4,5]
Output: 15

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
Topic Tags:
Solve Problems on Arrays [Easy -> Medium -> Hard]EasymaximumConsecutiveOnes
14px
Ln 1:Col 1
8 lines•124 chars
Spaces: 2•UTF-8
JS(Node v20.12)