-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2024. Maximize the Confusion of an Exam.cpp
49 lines (49 loc) · 1.51 KB
/
2024. Maximize the Confusion of an Exam.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Solution {
public:
int maxConsecutiveAnswers1(string answerKey, int k) {
int start=0;
int n = answerKey.size();
int fill=0;
int result=0;
for(int i=0;i<n;++i){
if(answerKey[i]=='F')
++fill;
while(fill>k){
if(answerKey[start] == 'F')
--fill;
++start;
}
result = max(result,i-start+1);
}
start = fill = 0;
for(int i=0;i<n;++i){
if(answerKey[i]=='T')
++fill;
while(fill>k){
if(answerKey[start] == 'T')
--fill;
++start;
}
result = max(result,i-start+1);
}
return result;
}
// single loop
int maxConsecutiveAnswers(string answerKey, int k) {
int maxSize = 0;
unordered_map<char, int> count;
for (int right = 0; right < answerKey.length(); right++) {
count[answerKey[right]]++;
int minor = min(count['T'], count['F']);
if (minor <= k) {
maxSize++;
} else {
count[answerKey[right - maxSize]]--;
}
}
return maxSize;
}
};