Skip to main content

Max Consecutive Ones II

LeetCode Link

Problem Description​

Visit LeetCode for the full problem description.


Solutions​

Solution 1: C# (Best: 235 ms)​

MetricValue
Runtime235 ms
Memory39.2 MB
Date2022-01-26
Solution
public class Solution {
public int FindMaxConsecutiveOnes(int[] nums) {
int prev = -1, current = 0, maxLen = 0;
for (int i = 0; i < nums.Length; i++)
{
if(nums[i] == 0)
{
prev = current;
current = 0;
}
else current++;

maxLen = Math.Max(maxLen, prev+1+current);
}
return maxLen;
}
}

Complexity Analysis​

ApproachTimeSpace
SolutionTo be analyzedTo be analyzed