Skip to content

Commit 5a96338

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

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
import collections
5+
6+
7+
class Solution(object):
8+
def maxConsecutiveAnswers(self, answerKey, k):
9+
"""
10+
:type answerKey: str
11+
:type k: int
12+
:rtype: int
13+
"""
14+
result = max_count = 0
15+
count = collections.Counter()
16+
for i in xrange(len(answerKey)):
17+
count[answerKey[i]] += 1
18+
max_count = max(max_count, count[answerKey[i]])
19+
if result-max_count >= k:
20+
count[answerKey[i-result]] -= 1
21+
else:
22+
result += 1
23+
return result

0 commit comments

Comments
 (0)