diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 05316d0..2d6cbae 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -222,4 +222,5 @@ mod p3144_minimum_substring_partition_of_equal_character_frequency; mod p3142_check_if_grid_satisfies_conditions; mod p3153_sum_of_digit_differences_of_all_pairs; mod p3127_make_a_square_with_the_same_color; -mod p1450_number_of_students_doing_homework_at_a_given_time; \ No newline at end of file +mod p1450_number_of_students_doing_homework_at_a_given_time; +mod p2024_maximize_the_confusion_of_an_exam; \ No newline at end of file diff --git a/src/problem/p2024_maximize_the_confusion_of_an_exam.rs b/src/problem/p2024_maximize_the_confusion_of_an_exam.rs new file mode 100644 index 0000000..11d97f0 --- /dev/null +++ b/src/problem/p2024_maximize_the_confusion_of_an_exam.rs @@ -0,0 +1,53 @@ +/** + * [2024] Maximize the Confusion of an Exam + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn max_consecutive_answers(answer_key: String, k: i32) -> i32 { + let str : Vec = answer_key.chars().collect(); + + Self::max_consecutive_char(&str, k, 'T').max(Self::max_consecutive_char(&str, k, 'F')) + } + + fn max_consecutive_char(str: &Vec, k: i32, c : char) -> i32 { + let n = str.len(); + let mut result = 0; + + let (mut left, mut sum) = (0, 0); + for right in 0..n { + sum += match str[right] == c { + true => 0, + false => 1 + }; + + while sum > k { + sum -= match str[left] == c { + true => 0, + false => 1 + }; + left += 1; + } + + result = result.max(right - left + 1); + } + + result as i32 + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2024() { + assert_eq!(4, Solution::max_consecutive_answers("TTFF".to_owned(), 2)); + assert_eq!(3, Solution::max_consecutive_answers("TFFT".to_owned(), 1)); + } +}