Max Consecutive Ones II
Problem Descriptionβ
Visit LeetCode for the full problem description.
Solutionsβ
Solution 1: C# (Best: 235 ms)β
| Metric | Value |
|---|---|
| Runtime | 235 ms |
| Memory | 39.2 MB |
| Date | 2022-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β
| Approach | Time | Space |
|---|---|---|
| Solution | To be analyzed | To be analyzed |