LeetCode #485 Easy

Max Consecutive Ones

Max Consecutive Ones is LeetCode 485 (Easy). Given a binary array nums, return the length of the longest run of 1s that sit next to each other.

  • Every element is either 0 or 1.
  • If the array has no 1 at all, the answer is 0.

The array holds up to 10⁵ elements, so the target is one pass.

Constraints
  • 1 <= nums.length <= 10⁵
  • nums[i] is either 0 or 1.
array
Open on LeetCode ↗
02

Intuition

A run of 1s grows by one with every 1 and is broken by every 0, so you never need to look back. Keep one counter, streak, for the run that ends at the current element: a 1 adds one to it, a 0 sets it back to 0.

The answer is the largest value streak ever reaches, so keep that in a second counter, best. Each element is read once, which gives O(n) time and O(1) space.

How to spot this pattern

This is the simplest sliding window: the window is the current run of 1s, and a 0 shrinks it to nothing. Any "longest run of elements that satisfy a condition" question can start from this counter.

03

Approach

Try it first

Before reading on: walk [1, 1, 0, 1, 1, 1] by hand and write the length of the run of 1s that ends at each index. Where does the answer appear, and what goes wrong if you only check the run when you reach a 0?

1

Start both counters at 0

Set best = streak = 0. Starting best at 0 is what makes an array with no 1s return 0, as it should.

2

Grow or reset the streak

For each n in nums:

  • n == 1: add one to streak, then set best = max(best, streak).
  • n == 0: set streak = 0; the next 1 starts a new run.
3

Return best

After the loop, best holds the longest run. Because it was updated on every 1, a run that reaches the last element is already counted, so no extra check after the loop is needed.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
▶3 best = streak = 0
▶4 for n in nums:
▶5 if n == 1:
▶6 streak += 1
▶7 best = max(best, streak)
▶8 else:
▶9 streak = 0
▶10 return best
nums101102131415streak0best0streak = 0, best = 0
streak0run ending here
best0longest so far
Start. streak is the length of the run of 1s that ends at the current element; best is the longest run seen. Both start at 0, so an array with no 1s returns 0.
nums101102131415longest runstreak1best1streak 1 beats best → best = 1
nums[0]1
streak1
best1
A 1 makes the run one longer: streak = 1. That is longer than any run before, so best = 1 right away, without waiting for a 0.
nums101102131415longest runstreak2best2streak 2 beats best → best = 2
nums[1]1
streak2
best2
A 1 makes the run one longer: streak = 2. That is longer than any run before, so best = 2 right away, without waiting for a 0.
nums101102131415longest runstreak0best20 → the run breaks, streak = 0
nums[2]0
streak0
best2
A 0 breaks the run of 2, so streak drops to 0. best already holds 2, so nothing is lost; the next 1 starts a new run.
nums101102131415longest runstreak1best2streak 1, best stays 2
nums[3]1
streak1
best2
A 1 makes the run one longer: streak = 1. It is not longer than the best run of 2, so best stays.
nums101102131415longest runstreak2best2streak 2, best stays 2
nums[4]1
streak2
best2
A 1 makes the run one longer: streak = 2. It is not longer than the best run of 2, so best stays.
nums101102131415longest runstreak3best3streak 3 beats best → best = 3
nums[5]1
streak3
best3
A 1 makes the run one longer: streak = 3. That is longer than any run before, so best = 3 right away, without waiting for a 0.
nums101102131415longest runstreak3best3return 3
answer3
Answer 3. The bracket marks the longest run. Because best was updated on every 1, a run that reaches the last element is already counted.
05

Common pitfalls

Updating best only when a 0 arrives

✗ Wrong
for n in nums:
    if n == 1:
        streak += 1
    else:
        best = max(best, streak)
        streak = 0
return best
✓ Right
for n in nums:
    if n == 1:
        streak += 1
        best = max(best, streak)
    else:
        streak = 0
return best

In [1, 0, 1, 1, 1] the last run never meets a 0, so it is never compared and the answer comes out as 1 instead of 3. Update best on every 1, or compare once more after the loop.

06

Complexity

Time
O(n)
Space
O(1)
One pass, two counters.
07

Max Consecutive Ones FAQ

What changes in the other problems of this series?
  • Max Consecutive Ones II (487): you may flip one 0. Track the run length before and after the last 0.
  • Max Consecutive Ones III (1004): you may flip up to k zeros. A 0 no longer ends the run; use a sliding window that shrinks from the left whenever it holds more than k zeros.