-
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 2225: Find Players With Zero or One Losses
- Loading branch information
Showing
3 changed files
with
97 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
60 changes: 60 additions & 0 deletions
60
src/problem_2225_find_players_with_zero_or_one_losses/hash_map.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,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
36
src/problem_2225_find_players_with_zero_or_one_losses/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,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); | ||
} | ||
} | ||
} |