problem:2024. 考试的最大困扰度
思路:T和F各求一遍滑动窗口维护不超过k个相异字符
Accode:
class Solution {
public:
int toggle(string answerKey,char c,int k){
int ans = 0;
// int len = answerKey.length();
deque<char> deq;
int tot = 0;
for(auto it:answerKey){
while(deq.size() && tot>=k && it==c){
if(deq.front()==c) tot--;
deq.pop_front();
}
deq.push_back(it);
if(it==c) tot++;
ans = max(ans,(int)deq.size());
}
return ans;
}
int maxConsecutiveAnswers(string answerKey, int k) {
return max(toggle(answerKey,'T',k),toggle(answerKey,'F',k));
}
};
时间复杂度:$o(n)$