diff --git a/src/lib.rs b/src/lib.rs index 10dca8ec..c387ba39 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1571,6 +1571,7 @@ pub mod problem_2215_find_the_difference_of_two_arrays; pub mod problem_2216_minimum_deletions_to_make_array_beautiful; pub mod problem_2220_minimum_bit_flips_to_convert_number; pub mod problem_2224_minimum_number_of_operations_to_convert_time; +pub mod problem_2225_find_players_with_zero_or_one_losses; pub mod problem_2226_maximum_candies_allocated_to_k_children; pub mod problem_2231_largest_number_after_digit_swaps_by_parity; pub mod problem_2235_add_two_integers; diff --git a/src/problem_2225_find_players_with_zero_or_one_losses/hash_map.rs b/src/problem_2225_find_players_with_zero_or_one_losses/hash_map.rs new file mode 100644 index 00000000..22c132ab --- /dev/null +++ b/src/problem_2225_find_players_with_zero_or_one_losses/hash_map.rs @@ -0,0 +1,60 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +use std::collections::hash_map::Entry; +use std::collections::{HashMap, HashSet}; + +impl Solution { + pub fn find_winners(matches: Vec>) -> Vec> { + let mut states = HashMap::new(); + let mut all_players = HashSet::new(); + + for match_ in matches { + let [winner, loser]: [_; 2] = match_.try_into().ok().unwrap(); + let (winner, loser) = (winner as u32, loser as u32); + + all_players.extend([winner, loser]); + + match states.entry(loser) { + Entry::Occupied(entry) => *entry.into_mut() = true, + Entry::Vacant(entry) => { + entry.insert(false); + } + } + } + + let mut all_players = all_players.into_iter().collect::>(); + + all_players.sort_unstable(); + + let mut no_loses = Vec::new(); + let mut one_loses = Vec::new(); + + for &player in &*all_players { + match states.get(&player) { + None => no_loses.push(player as i32), + Some(false) => one_loses.push(player as i32), + Some(true) => {} + } + } + + vec![no_loses, one_loses] + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn find_winners(matches: Vec>) -> Vec> { + Self::find_winners(matches) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2225_find_players_with_zero_or_one_losses/mod.rs b/src/problem_2225_find_players_with_zero_or_one_losses/mod.rs new file mode 100644 index 00000000..e7749b10 --- /dev/null +++ b/src/problem_2225_find_players_with_zero_or_one_losses/mod.rs @@ -0,0 +1,36 @@ +pub mod hash_map; + +pub trait Solution { + fn find_winners(matches: Vec>) -> Vec>; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + ( + &[ + [1, 3], + [2, 3], + [3, 6], + [5, 6], + [5, 7], + [4, 5], + [4, 8], + [4, 9], + [10, 4], + [10, 9], + ] as &[_], + [&[1, 2, 10] as &[_], &[4, 5, 7, 8]], + ), + (&[[2, 3], [1, 3], [5, 4], [6, 4]], [&[1, 2, 5, 6], &[]]), + (&[[1, 100_000]], [&[1], &[100_000]]), + ]; + + for (matches, expected) in test_cases { + assert_eq!(S::find_winners(matches.iter().map(Vec::from).collect()), expected); + } + } +}