Max Consecutive Ones - Problem
Find the Longest Streak of Consecutive 1's!
You're given a binary array
Goal: Return the length of the longest unbroken sequence of 1's.
Example: In the array
This is a classic problem that tests your ability to traverse an array while keeping track of streaks and maximums!
You're given a binary array
nums containing only 0's and 1's. Your task is to find the maximum number of consecutive 1's in the array.Goal: Return the length of the longest unbroken sequence of 1's.
Example: In the array
[1,1,0,1,1,1], the longest consecutive sequence of 1's has length 3.This is a classic problem that tests your ability to traverse an array while keeping track of streaks and maximums!
Input & Output
example_1.py โ Basic Case
$
Input:
[1,1,0,1,1,1]
โบ
Output:
3
๐ก Note:
The longest sequence of consecutive 1's is [1,1,1] at the end, which has length 3.
example_2.py โ Single Element
$
Input:
[1,0,1,1,0,1]
โบ
Output:
2
๐ก Note:
The longest sequence of consecutive 1's is [1,1] in the middle, which has length 2.
example_3.py โ All Zeros
$
Input:
[0,0,0]
โบ
Output:
0
๐ก Note:
There are no 1's in the array, so the maximum consecutive 1's is 0.
Constraints
- 1 โค nums.length โค 105
- nums[i] is either 0 or 1
- The array contains only binary values
Visualization
Tap to expand
Understanding the Visualization
1
Track Current Streak
Keep count of consecutive running days (1's)
2
Reset on Rest Day
When you don't run (0), reset current streak to 0
3
Remember Best Streak
Always remember your longest streak so far
4
Continue Until End
Process the entire log to find the absolute maximum
Key Takeaway
๐ฏ Key Insight: We only need to track two numbers - our current streak and our best streak ever. When we hit a 0, we reset the current streak but keep our record!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code