1004. Max Consecutive Ones III
June 24, 2025
04:32 AM
No headings found
Loading content...
Related Posts
Leetcode
No headings found
Related Posts
Leetcode
Problem
Approach
Solution
1function longestOnes(nums: number[], k: number): number {
2 let left = 0;
3 let max_length = 0;
4 let zero_count = 0;
5
6 for(let right = 0; right < nums.length; right++) {
7 if(nums[right] === 0) {
8 zero_count++;
9 }
10
11 while(zero_zount > k) {
12 if(nums[left] === 0) {
13 zero_count--;
14 }
15 left++
16 }
17 max_length = Math.max(max_length, right - left + 1);
18 }
19 return max_length;
20}