-
Notifications
You must be signed in to change notification settings - Fork 3
/
2024.cpp
30 lines (29 loc) · 850 Bytes
/
2024.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
int maxConsecutiveAnswers(string answerKey, int k) {
int res = 0;
int left = 0;
int right = 0;
int count = 0;
// to flip T to F
for (; right < answerKey.size(); ++right) {
if (answerKey[right] == 'T') count++;
if (count > k) {
if (answerKey[left] == 'T') count--;
left++;
}
res = max(res, right - left + 1);
}
// to flip F to T
right = left = count = 0;
for (; right < answerKey.size(); ++right) {
if (answerKey[right] == 'F') count++;
if (count > k) {
if (answerKey[left] == 'F') count--;
left++;
}
res = max(res, right - left + 1);
}
return res;
}
};