Skip to content

Commit

Permalink
Add problem 2225: Find Players With Zero or One Losses
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Jul 5, 2024
1 parent ddfffac commit 979faa7
Show file tree
Hide file tree
Showing 3 changed files with 97 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 @@ -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;
Expand Down
60 changes: 60 additions & 0 deletions src/problem_2225_find_players_with_zero_or_one_losses/hash_map.rs
Original file line number Diff line number Diff line change
@@ -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<i32>>) -> Vec<Vec<i32>> {
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::<Box<_>>();

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<i32>>) -> Vec<Vec<i32>> {
Self::find_winners(matches)
}
}

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

pub trait Solution {
fn find_winners(matches: Vec<Vec<i32>>) -> Vec<Vec<i32>>;
}

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

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

0 comments on commit 979faa7

Please sign in to comment.