2024. Maximize the Confusion of an Exam (Medium) - TengnanYao/daily_leetcode GitHub Wiki

class Solution:
    def maxConsecutiveAnswers(self, answerKey: str, k: int) -> int:
        n = len(answerKey)
        if k > n // 2:
            return n
        result = 0
        t, f = [], []
        i_t = i_f = -1
        for i in range(n):
            if answerKey[i] == "T":
                t.append(i)
                if len(t) > k:
                    i_t = t.pop(0)
            else:
                f.append(i)
                if len(f) > k:
                    i_f = f.pop(0)
            result = max(result, i - i_t, i - i_f)
        return result