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.
- 1 <= nums.length <= 10⁵
- nums[i] is either 0 or 1.
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.
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.
Approach
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?
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.
Grow or reset the streak
For each n in nums:
n == 1: add one tostreak, then setbest = max(best, streak).n == 0: setstreak = 0; the next 1 starts a new run.
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.
Code in Python, C++ and Java
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.streak = 1. That is longer than any run before, so best = 1 right away, without waiting for a 0.streak = 2. That is longer than any run before, so best = 2 right away, without waiting for a 0.streak drops to 0. best already holds 2, so nothing is lost; the next 1 starts a new run.streak = 1. It is not longer than the best run of 2, so best stays.streak = 2. It is not longer than the best run of 2, so best stays.streak = 3. That is longer than any run before, so best = 3 right away, without waiting for a 0.best was updated on every 1, a run that reaches the last element is already counted.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.streak = 1. That is longer than any run before, so best = 1 right away, without waiting for a 0.streak drops to 0. best already holds 1, so nothing is lost; the next 1 starts a new run.streak = 1. It is not longer than the best run of 1, so best stays.streak = 2. That is longer than any run before, so best = 2 right away, without waiting for a 0.streak drops to 0. best already holds 2, so nothing is lost; the next 1 starts a new run.streak = 1. It is not longer than the best run of 2, so best stays.best was updated on every 1, a run that reaches the last element is already counted.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.streak stays 0.streak stays 0.streak stays 0.best never left 0.Common pitfalls
Updating best only when a 0 arrives
for n in nums:
if n == 1:
streak += 1
else:
best = max(best, streak)
streak = 0
return bestfor n in nums:
if n == 1:
streak += 1
best = max(best, streak)
else:
streak = 0
return bestIn [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.
Complexity
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
kzeros. A 0 no longer ends the run; use a sliding window that shrinks from the left whenever it holds more thankzeros.