-
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 1921: Eliminate Maximum Number of Monsters
- Loading branch information
Showing
3 changed files
with
66 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
43 changes: 43 additions & 0 deletions
43
src/problem_1921_eliminate_maximum_number_of_monsters/buckets.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,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
22
src/problem_1921_eliminate_maximum_number_of_monsters/mod.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,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); | ||
} | ||
} | ||
} |