Skip to content
LC-2024 Medium LeetCode

2024. Maximize the Confusion of an Exam

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 68% Topics: String, Binary Search, Sliding Window, Prefix Sum
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

import collections


class Solution(object):
    def maxConsecutiveAnswers(self, answerKey, k):
        """
        :type answerKey: str
        :type k: int
        :rtype: int
        """
        result = max_count = 0
        count = collections.Counter()
        for i in xrange(len(answerKey)):
            count[answerKey[i]] += 1
            max_count = max(max_count, count[answerKey[i]])
            if result-max_count >= k:
                count[answerKey[i-result]] -= 1
            else:
                result += 1
        return result

Solution from kamyu104/LeetCode-Solutions · MIT