Skip to content

Commit

Permalink
Add problem 2410: Maximum Matching of Players With Trainers
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Jan 25, 2025
1 parent 32f9b42 commit 34e5c8e
Show file tree
Hide file tree
Showing 3 changed files with 73 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 @@ -1794,6 +1794,7 @@ pub mod problem_2404_most_frequent_even_element;
pub mod problem_2405_optimal_partition_of_string;
pub mod problem_2406_divide_intervals_into_minimum_number_of_groups;
pub mod problem_2409_count_days_spent_together;
pub mod problem_2410_maximum_matching_of_players_with_trainers;

#[cfg(test)]
mod test_utilities;
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 src/problem_2410_maximum_matching_of_players_with_trainers/mod.rs
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,
);
}
}
}

0 comments on commit 34e5c8e

Please sign in to comment.