Skip to content

Commit 550629d

Browse files
authored
Create maximize-the-confusion-of-an-exam.cpp
1 parent 5e2c021 commit 550629d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int maxConsecutiveAnswers(string answerKey, int k) {
7+
int result = 0, max_count = 0;
8+
unordered_map<char, int> count;
9+
for (int i = 0; i < size(answerKey); ++i) {
10+
++count[answerKey[i]];
11+
max_count = max(max_count, count[answerKey[i]]);
12+
if (result - max_count >= k) {
13+
--count[answerKey[i - result]];
14+
} else {
15+
++result;
16+
}
17+
}
18+
return result;
19+
}
20+
};

0 commit comments

Comments
 (0)