公司:美团
类型:滑动窗口
题目
题意:从左往右统计改变已经使用0转换为1的个数,超过k个时就要把左指针往右移动,直到转换的次数小于等于k
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5+5;
int arr[N];
int main(){
int n, k;
scanf("%d%d", &n,&k);
for(int i = 0; i < n; i++){
scanf("%d", &arr[i]);
}
int cnt = 0, res = 0;
for(int i = 0, j = 0; i < n; i++){
if(arr[i] == 0) cnt++;
while(cnt > k){
if(arr[j] == 0) cnt--;
j++;
}
res = max(res, i-j+1);
}
printf("%d\n", res);
return 0;
}