From b1c26ca74eb4aa2d2fe26743c601abf7bcd3df80 Mon Sep 17 00:00:00 2001 From: EFanZh Date: Fri, 8 Dec 2023 19:39:09 +0800 Subject: [PATCH] Add problem 1921: Eliminate Maximum Number of Monsters --- src/lib.rs | 1 + .../buckets.rs | 43 +++++++++++++++++++ .../mod.rs | 22 ++++++++++ 3 files changed, 66 insertions(+) create mode 100644 src/problem_1921_eliminate_maximum_number_of_monsters/buckets.rs create mode 100644 src/problem_1921_eliminate_maximum_number_of_monsters/mod.rs diff --git a/src/lib.rs b/src/lib.rs index a2c02cfa..60bdd795 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1356,6 +1356,7 @@ pub mod problem_1911_maximum_alternating_subsequence_sum; pub mod problem_1913_maximum_product_difference_between_two_pairs; pub mod problem_1915_number_of_wonderful_substrings; pub mod problem_1920_build_array_from_permutation; +pub mod problem_1921_eliminate_maximum_number_of_monsters; pub mod problem_1922_count_good_numbers; pub mod problem_1926_nearest_exit_from_entrance_in_maze; pub mod problem_1929_concatenation_of_array; diff --git a/src/problem_1921_eliminate_maximum_number_of_monsters/buckets.rs b/src/problem_1921_eliminate_maximum_number_of_monsters/buckets.rs new file mode 100644 index 00000000..e04d15b9 --- /dev/null +++ b/src/problem_1921_eliminate_maximum_number_of_monsters/buckets.rs @@ -0,0 +1,43 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn eliminate_maximum(dist: Vec, speed: Vec) -> i32 { + let mut buckets = vec![0_u32; dist.len()].into_boxed_slice(); + + for (distance, speed) in dist.into_iter().zip(speed) { + if let Some(count) = buckets.get_mut(((distance - 1) as u32 / speed as u32) as usize) { + *count += 1; + } + } + + let mut need_to_kill = 0; + + for (can_kill, &count) in (1..).zip(&*buckets) { + need_to_kill += count; + + if need_to_kill > can_kill { + return can_kill as _; + } + } + + buckets.len() as _ + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn eliminate_maximum(dist: Vec, speed: Vec) -> i32 { + Self::eliminate_maximum(dist, speed) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_1921_eliminate_maximum_number_of_monsters/mod.rs b/src/problem_1921_eliminate_maximum_number_of_monsters/mod.rs new file mode 100644 index 00000000..4a120984 --- /dev/null +++ b/src/problem_1921_eliminate_maximum_number_of_monsters/mod.rs @@ -0,0 +1,22 @@ +pub mod buckets; + +pub trait Solution { + fn eliminate_maximum(dist: Vec, speed: Vec) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + ((&[1, 3, 4] as &[_], &[1, 1, 1] as &[_]), 3), + ((&[1, 1, 2, 3], &[1, 1, 1, 1]), 1), + ((&[3, 2, 4], &[5, 3, 2]), 1), + ]; + + for ((dist, speed), expected) in test_cases { + assert_eq!(S::eliminate_maximum(dist.to_vec(), speed.to_vec()), expected); + } + } +}