Skip to content

Commit

Permalink
Add problem 1921: Eliminate Maximum Number of Monsters
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Dec 8, 2023
1 parent 5a54621 commit b1c26ca
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
43 changes: 43 additions & 0 deletions src/problem_1921_eliminate_maximum_number_of_monsters/buckets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
pub fn eliminate_maximum(dist: Vec<i32>, speed: Vec<i32>) -> 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<i32>, speed: Vec<i32>) -> i32 {
Self::eliminate_maximum(dist, speed)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
22 changes: 22 additions & 0 deletions src/problem_1921_eliminate_maximum_number_of_monsters/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub mod buckets;

pub trait Solution {
fn eliminate_maximum(dist: Vec<i32>, speed: Vec<i32>) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
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);
}
}
}

0 comments on commit b1c26ca

Please sign in to comment.