leetcode每日一题-2024. 考试的最大困扰度
滑动窗口问题。
寻找最大长度的窗口,窗口内 'T' 或 'F'的数量为k
class Solution {
public:
string s;
int n;
int gk;
int getCnt(char c){
int ans=0;
int j=0,cnt=0;
for(int i=0;i<n;i++){
if(s[i]==c) cnt++;
while(cnt>gk){
if(s[j]==c) cnt--;
j++; //i在前,j在后,字符统计数量大于k区间缩小,j前进
}
ans= max(ans,i-j+1);//返回区间内字符数量,此时区间内c的数量为k
}
return ans;
}
int maxConsecutiveAnswers(string answerKey, int k) {
n=answerKey.size();
s=answerKey;
gk=k;
return max(getCnt('F'),getCnt('T'));
}
};