1004. Max Consecutive Ones III, Medium, 2 pointers
- since we can switch up to
k
elements therefore we will prepare a sliding window ofk
size. - meaning, we will trigger the internal while loop only when
count_of_zero > k
code
int longestOnes(vector<int>& nums, int k) {
int count = 0;
int ans = 0;
int l = 0;
for (int r = 0; r < nums.size(); r++) {
count += nums[r] == 0;
while (count > k and l <= r) {
count -= nums[l] == 0;
l++;
}
ans = max(ans, r - l + 1);
}
return ans;
}