Max Consecutive Ones
đź’» Try it yourself first. Write your solution in Java, C++, Python or JavaScript and run it against hidden tests, right here.On this page
Revision notes
Max Consecutive Ones
Video: “Max Consecutive Ones: why your code returns 2, not 3” on LearnWithKiriti (Kodee). LeetCode 485.
Return the length of the longest run of 1s in an array of 0s and 1s.
Example: [1, 1, 0, 1, 1, 1] -> 3
| Folder | Approach | Time | Extra space |
|---|---|---|---|
Brute/ |
Brute force: From every start, count the 1s that follow, and keep the biggest count. The same 1s get counted again and again. | O(n²) worst (all 1s) | O(1) |
Optimal/ |
Optimal: One pass: current counts the run you are in, a 0 resets it, and maxCount is updated inside the 1 branch, so a run that ends the array is not lost. | O(n) | O(1) |
Rebuild it
Count the run you are in; a 0 resets it. The classic bug updates maxCount only when a 0 arrives, so a run that ends the array (the 3 here) is never saved: it returns 2. Update maxCount inside the 1 branch instead.
See each folder’s pseudocode.md. Every program runs Example 1 and prints the answer; the same code is in the practice panel’s Start menu on learnwithkiriti.com, where Shelf Bot can act it out.
Brute
Brute: count from every start
From every start, count the 1s that follow, and keep the biggest count. The same 1s get counted again and again.
maxCount = 0
for start from 0 to size - 1:
count = 0
scan = start
while scan < size and nums[scan] == 1:
count = count + 1
scan = scan + 1
maxCount = max(maxCount, count)
return maxCount
O(n²) when the array is all 1s: every start counts the same 1s again.
import java.util.*;
public class MaxConsecutiveOnesBrute {
public int findMaxConsecutiveOnes(int[] nums) {
int maxCount = 0;
for (int start = 0; start < nums.length; start++) {
int count = 0;
int scan = start;
while (scan < nums.length && nums[scan] == 1) {
count++;
scan++;
}
maxCount = Math.max(maxCount, count);
}
return maxCount;
}
public static void main(String[] args) {
MaxConsecutiveOnesBrute solver = new MaxConsecutiveOnesBrute();
int[] nums = new int[]{1, 1, 0, 1, 1, 1};
int answer = solver.findMaxConsecutiveOnes(nums);
System.out.println(answer);
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int maxCount = 0;
for (int start = 0; start < (int) nums.size(); start++) {
int count = 0;
int scan = start;
while (scan < (int) nums.size() && nums[scan] == 1) {
count++;
scan++;
}
maxCount = max(maxCount, count);
}
return maxCount;
}
};
void print(const vector<int>& values) {
cout << "[";
for (size_t spot = 0; spot < values.size(); spot++) {
if (spot > 0) {
cout << ", ";
}
cout << values[spot];
}
cout << "]" << endl;
}
void print(const vector<vector<int>>& grid) {
for (const vector<int>& line : grid) {
print(line);
}
}
int main() {
Solution solver;
vector<int> nums = {1, 1, 0, 1, 1, 1};
cout << solver.findMaxConsecutiveOnes(nums) << endl;
return 0;
}def find_max_consecutive_ones(nums):
max_count = 0
for start in range(len(nums)):
count = 0
scan = start
while scan < len(nums) and nums[scan] == 1:
count += 1
scan += 1
max_count = max(max_count, count)
return max_count
if __name__ == "__main__":
nums = [1, 1, 0, 1, 1, 1]
print(find_max_consecutive_ones(nums))function findMaxConsecutiveOnes(nums) {
let maxCount = 0;
for (let start = 0; start < nums.length; start++) {
let count = 0;
let scan = start;
while (scan < nums.length && nums[scan] === 1) {
count++;
scan++;
}
maxCount = Math.max(maxCount, count);
}
return maxCount;
}
const nums = [1, 1, 0, 1, 1, 1];
console.log(findMaxConsecutiveOnes(nums));Optimal
Optimal: one pass
One pass: current counts the run you are in, a 0 resets it, and maxCount is updated inside the 1 branch, so a run that ends the array is not lost.
maxCount = 0
current = 0
for each light in nums:
if light == 1:
current = current + 1
if current > maxCount: maxCount = current
else:
current = 0
return maxCount
Dry run on Example 1:
| light | current | maxCount |
|---|---|---|
| 1 | 1 | 1 |
| 1 | 2 | 2 |
| 0 | 0 | 2 |
| 1 | 1 | 2 |
| 1 | 2 | 2 |
| 1 | 3 | 3 |
O(n) time, O(1) extra space.
import java.util.*;
public class MaxConsecutiveOnesOptimal {
public int findMaxConsecutiveOnes(int[] nums) {
int maxCount = 0;
int current = 0;
for (int light : nums) {
if (light == 1) {
current++;
if (current > maxCount) {
maxCount = current;
}
} else {
current = 0;
}
}
return maxCount;
}
public static void main(String[] args) {
MaxConsecutiveOnesOptimal solver = new MaxConsecutiveOnesOptimal();
int[] nums = new int[]{1, 1, 0, 1, 1, 1};
int answer = solver.findMaxConsecutiveOnes(nums);
System.out.println(answer);
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int maxCount = 0;
int current = 0;
for (int light : nums) {
if (light == 1) {
current++;
if (current > maxCount) {
maxCount = current;
}
} else {
current = 0;
}
}
return maxCount;
}
};
void print(const vector<int>& values) {
cout << "[";
for (size_t spot = 0; spot < values.size(); spot++) {
if (spot > 0) {
cout << ", ";
}
cout << values[spot];
}
cout << "]" << endl;
}
void print(const vector<vector<int>>& grid) {
for (const vector<int>& line : grid) {
print(line);
}
}
int main() {
Solution solver;
vector<int> nums = {1, 1, 0, 1, 1, 1};
cout << solver.findMaxConsecutiveOnes(nums) << endl;
return 0;
}def find_max_consecutive_ones(nums):
max_count = 0
current = 0
for light in nums:
if light == 1:
current += 1
if current > max_count:
max_count = current
else:
current = 0
return max_count
if __name__ == "__main__":
nums = [1, 1, 0, 1, 1, 1]
print(find_max_consecutive_ones(nums))function findMaxConsecutiveOnes(nums) {
let maxCount = 0;
let current = 0;
for (const light of nums) {
if (light === 1) {
current++;
if (current > maxCount) {
maxCount = current;
}
} else {
current = 0;
}
}
return maxCount;
}
const nums = [1, 1, 0, 1, 1, 1];
console.log(findMaxConsecutiveOnes(nums));