Skip to content

Commit fcd904c

Browse files
committed
Time: 1 ms (96.55%) Space: 58.7 MB (41.38%)
1 parent c3114f4 commit fcd904c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function findMaxConsecutiveOnes(nums: number[]): number {
2+
const n = nums.length;
3+
let left = 0;
4+
let totalZeros = 0;
5+
let maxLength = 0;
6+
for (let right = 0; right < n; right++) {
7+
const curr = nums[right];
8+
if (curr === 0) {
9+
totalZeros++;
10+
}
11+
while (totalZeros > 1) {
12+
if (nums[left] === 0) {
13+
totalZeros--;
14+
}
15+
left++;
16+
}
17+
maxLength = Math.max(maxLength, right - left + 1);
18+
}
19+
return maxLength;
20+
};

0 commit comments

Comments
 (0)