力扣
解题报告:
因为只有T和F两个元素,不难证明单向性。尺取法解决。当然这题也可以二分。
AC代码:
class Solution {
public:int maxConsecutiveAnswers(string answerKey, int k) {int l = 0, r = 0;int T = 0, F = 0;int ans = -1;while(r < answerKey.size()) {if(answerKey[r] == 'F') F++;else T++;while(min(F,T) > k) {if(answerKey[l] == 'F') F--;else T--;l++;}ans = max(ans, r-l+1);r++;}return ans;}
};