-
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 2410: Maximum Matching of Players With Trainers
- Loading branch information
Showing
3 changed files
with
73 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
48 changes: 48 additions & 0 deletions
48
src/problem_2410_maximum_matching_of_players_with_trainers/greedy.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,48 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn match_players_and_trainers(players: Vec<i32>, trainers: Vec<i32>) -> i32 { | ||
let mut players = players.into_iter().map(|x| x as u32).collect::<Vec<_>>(); | ||
let mut trainers = trainers.into_iter().map(|x| x as u32).collect::<Vec<_>>(); | ||
|
||
players.sort_unstable(); | ||
trainers.sort_unstable(); | ||
|
||
let mut trainer_iter = trainers.iter().copied(); | ||
let mut result = 0; | ||
|
||
'outer: for &player in &players { | ||
loop { | ||
if let Some(trainer) = trainer_iter.next() { | ||
if trainer >= player { | ||
break; | ||
} | ||
} else { | ||
break 'outer; | ||
} | ||
} | ||
|
||
result += 1; | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn match_players_and_trainers(players: Vec<i32>, trainers: Vec<i32>) -> i32 { | ||
Self::match_players_and_trainers(players, trainers) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/problem_2410_maximum_matching_of_players_with_trainers/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,24 @@ | ||
pub mod greedy; | ||
|
||
pub trait Solution { | ||
fn match_players_and_trainers(players: Vec<i32>, trainers: Vec<i32>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
((&[4, 7, 9] as &[_], &[8, 2, 5, 8] as &[_]), 2), | ||
((&[1, 1, 1], &[10]), 1), | ||
]; | ||
|
||
for ((players, trainers), expected) in test_cases { | ||
assert_eq!( | ||
S::match_players_and_trainers(players.to_vec(), trainers.to_vec()), | ||
expected, | ||
); | ||
} | ||
} | ||
} |