20240902 finished.

This commit is contained in:
jackfiled 2024-09-02 11:59:40 +08:00
parent 421be979fe
commit 828e1f910d
2 changed files with 55 additions and 1 deletions

View File

@ -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;
mod p1450_number_of_students_doing_homework_at_a_given_time;
mod p2024_maximize_the_confusion_of_an_exam;

View File

@ -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<char> = 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<char>, 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));
}
}