|
| 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 | + */ |
| 43 | +pub 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 | + |
| 50 | +impl Solution { |
| 51 | + // Credit: https://leetcode.com/problems/maximize-the-confusion-of-an-exam/solutions/3729580/optimal-rust-solution/ |
| 52 | + pub fn max_consecutive_answers(answer_key: String, k: i32) -> i32 { |
| 53 | + let (mut trues, mut falses) = (0i32, 0i32); |
| 54 | + let answer_key = answer_key.into_bytes(); |
| 55 | + |
| 56 | + let (mut left, mut right) = (0, 0); |
| 57 | + let mut result = 0; |
| 58 | + |
| 59 | + while right < answer_key.len() { |
| 60 | + if answer_key[right] == b'T' { |
| 61 | + trues += 1 |
| 62 | + } else { |
| 63 | + falses += 1 |
| 64 | + } |
| 65 | + |
| 66 | + while left < right && i32::min(falses, trues) > k { |
| 67 | + if answer_key[left] == b'T' { |
| 68 | + trues -= 1 |
| 69 | + } else { |
| 70 | + falses -= 1 |
| 71 | + } |
| 72 | + left += 1; |
| 73 | + } |
| 74 | + |
| 75 | + result = usize::max(result, right - left + 1); |
| 76 | + right += 1; |
| 77 | + } |
| 78 | + |
| 79 | + result as _ |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// submission codes end |
| 84 | + |
| 85 | +#[cfg(test)] |
| 86 | +mod tests { |
| 87 | + use super::*; |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn test_2024_example_1() { |
| 91 | + let answer_key = "TTFF".to_string(); |
| 92 | + let k = 2; |
| 93 | + |
| 94 | + let result = 4; |
| 95 | + |
| 96 | + assert_eq!(Solution::max_consecutive_answers(answer_key, k), result); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test_2024_example_2() { |
| 101 | + let answer_key = "TFFT".to_string(); |
| 102 | + let k = 1; |
| 103 | + |
| 104 | + let result = 3; |
| 105 | + |
| 106 | + assert_eq!(Solution::max_consecutive_answers(answer_key, k), result); |
| 107 | + } |
| 108 | + |
| 109 | + #[test] |
| 110 | + fn test_2024_example_3() { |
| 111 | + let answer_key = "TTFTTFTT".to_string(); |
| 112 | + let k = 1; |
| 113 | + |
| 114 | + let result = 5; |
| 115 | + |
| 116 | + assert_eq!(Solution::max_consecutive_answers(answer_key, k), result); |
| 117 | + } |
| 118 | +} |
0 commit comments