-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2024: Maximize the Confusion of an Exam
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
pub mod sliding_window; | ||
|
||
pub trait Solution { | ||
fn max_consecutive_answers(answer_key: String, k: i32) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [(("TTFF", 2), 4), (("TFFT", 1), 3), (("TTFTTFTT", 1), 5)]; | ||
|
||
for ((answer_key, k), expected) in test_cases { | ||
assert_eq!(S::max_consecutive_answers(answer_key.to_string(), k), expected); | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/problem_2024_maximize_the_confusion_of_an_exam/sliding_window.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn max_consecutive_answers(answer_key: String, k: i32) -> i32 { | ||
let answer_key = answer_key.into_bytes(); | ||
let k = k as u32; | ||
let mut window_start = 0; | ||
let mut window_false_count = 0; | ||
let mut window_true_count = 0; | ||
|
||
for &c in &answer_key { | ||
if c == b'F' { | ||
window_false_count += 1; | ||
} else { | ||
window_true_count += 1; | ||
} | ||
|
||
if window_false_count.min(window_true_count) > k { | ||
if answer_key[window_start] == b'F' { | ||
window_false_count -= 1; | ||
} else { | ||
window_true_count -= 1; | ||
} | ||
|
||
window_start += 1; | ||
} | ||
} | ||
|
||
(answer_key.len() - window_start) as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn max_consecutive_answers(answer_key: String, k: i32) -> i32 { | ||
Self::max_consecutive_answers(answer_key, k) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |