↓Skip to main content
  1. LeetCode/

LeetCode 2024: Maximize the Confusion of an Exam

·2 mins· ·
LeetCode Medium Sliding-Window String Binary-Search Prefix-Sum
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-10

  • Submissions: Accepted (first try)

Approach
#

Making a stretch of answers all the same needs at least as many changes as the rarer of T and F. So a window is valid ⇔ min(count of T, count of F) <= k.

The rarer count only grows or stays as the window grows, so the condition is monotonic: use the “longest” sliding window and shrink the left when invalid.

My Solution
#

class Solution:
    def maxConsecutiveAnswers(self, answerKey: str, k: int) -> int:
        cnt = Counter()
        left = best = 0
        for right, c in enumerate(answerKey):
            cnt[c] += 1
            while min(cnt["T"], cnt["F"]) > k:     # invalid only if both exceed k
                cnt[answerKey[left]] -= 1
                left += 1
            best = max(best, right - left + 1)
        return best

My original counted only T, with the condition count_true > k and (length - count_true) > k, which means the same thing.

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Running two passes, one for “all T” and one for “all F”, also works, but one window looking at the rarer letter is enough.

Interview Explanation
#

To make a stretch identical, the fewest changes is the rarer of T and F, so a window is valid when that count is <= k. The condition is monotonic in the window size, so I slide a window, shrink the left when invalid, and track the longest length. Time O(n).

Related

LeetCode 1208: Get Equal Substrings Within Budget
·1 min
LeetCode Medium Sliding-Window String Binary-Search Prefix-Sum
LeetCode 1208 note: each position costs |s[i] - t[i]|, so the problem becomes the longest subarray with sum <= maxCost
LeetCode 3694: Distinct Points Reachable After Substring Removal
·2 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table String
LeetCode 3694 note: the endpoint is the total displacement minus the removed segment’s displacement; a length-k window tracks that segment and each endpoint goes into a set
LeetCode 209: Minimum Size Subarray Sum (Sliding Window)
·1 min
LeetCode Daily Medium Array Sliding-Window Two-Pointers Prefix-Sum Binary-Search
Sliding window with two pointers; minimize subarray length where sum ≥ target.
LeetCode 3634: Minimum Removals to Balance Array
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 3634 note: after sorting, what remains is always a contiguous block; for each minimum nums[left], a right-only pointer finds how far it can extend, and everything outside is removed
LeetCode 1016: Binary String With Substrings Representing 1 To N
·3 mins
LeetCode Medium Sliding-Window String Bit-Manipulation Hash-Table
LeetCode 1016 note: s has at most 1000 characters, so there are few substrings of any fixed length and a large n must be False; checking from n downward fails fast, or extend up to 30 bits from each start to collect every value <= n
LeetCode 1888: Minimum Number of Flips to Make the Binary String Alternating
·2 mins
LeetCode Medium Sliding-Window String Dynamic-Programming
LeetCode 1888 note: rotations become length-n windows over s + s; the window’s mismatches against 0101… are diff, and against 1010… they are n - diff