2024. Maximize the Confusion of an Exam Medium

@problem@discussion
#String#Binary Search#Sliding Window#Prefix Sum



1/**
2 * [2024] Maximize the Confusion of an Exam
3 *
4 * A teacher is writing a test with n true/false questions, with 'T' denoting true and 'F' denoting false. He wants to confuse the students by maximizing the number of consecutive questions with the same answer (multiple trues or multiple falses in a row).
5 * You are given a string answerKey, where answerKey[i] is the original answer to the i^th question. In addition, you are given an integer k, the maximum number of times you may perform the following operation:
6 * 
7 * 	Change the answer key for any question to 'T' or 'F' (i.e., set answerKey[i] to 'T' or 'F').
8 * 
9 * Return the maximum number of consecutive 'T's or 'F's in the answer key after performing the operation at most k times.
10 *  
11 * Example 1:
12 * 
13 * Input: answerKey = "TTFF", k = 2
14 * Output: 4
15 * Explanation: We can replace both the 'F's with 'T's to make answerKey = "<u>TTTT</u>".
16 * There are four consecutive 'T's.
17 * 
18 * Example 2:
19 * 
20 * Input: answerKey = "TFFT", k = 1
21 * Output: 3
22 * Explanation: We can replace the first 'T' with an 'F' to make answerKey = "<u>FFF</u>T".
23 * Alternatively, we can replace the second 'T' with an 'F' to make answerKey = "T<u>FFF</u>".
24 * In both cases, there are three consecutive 'F's.
25 * 
26 * Example 3:
27 * 
28 * Input: answerKey = "TTFTTFTT", k = 1
29 * Output: 5
30 * Explanation: We can replace the first 'F' to make answerKey = "<u>TTTTT</u>FTT"
31 * Alternatively, we can replace the second 'F' to make answerKey = "TTF<u>TTTTT</u>". 
32 * In both cases, there are five consecutive 'T's.
33 * 
34 *  
35 * Constraints:
36 * 
37 * 	n == answerKey.length
38 * 	1 <= n <= 5 * 10^4
39 * 	answerKey[i] is either 'T' or 'F'
40 * 	1 <= k <= n
41 * 
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/maximize-the-confusion-of-an-exam/
46// discuss: https://leetcode.com/problems/maximize-the-confusion-of-an-exam/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51    pub fn max_consecutive_answers(answer_key: String, k: i32) -> i32 {
52        0
53    }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_2024() {
64    }
65}
66


Back
© 2025 bowen.ge All Rights Reserved.