2024. 考试的最大困扰度
双指针扫描一遍记录滑动窗口内的T和F的数量,如果都超过则减少其中一个数量至合法
时间复杂度:O(n)O(n)O(n),只扫描一遍非常快
class Solution {
public:
int maxConsecutiveAnswers(string a, int k) {
int l = 0, r = 0, tc = 0, fc = 0, n = a.size();
int res = 0;
while (r < n) {
if (a[r] == 'T') tc ++ ;
else fc ++ ;
while (tc > k && fc > k) {
if (a[l] == 'T') tc -- ;
else fc -- ;
l ++ ;
}
res = max(res, r - l + 1);
r ++ ;
}
return res;
}
};
1万+



被折叠的 条评论
为什么被折叠?



