-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 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
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,56 @@ | ||
pub struct Solution; | ||
|
||
use std::collections::HashMap; | ||
|
||
impl Solution { | ||
pub fn loud_and_rich(richer: Vec<Vec<i32>>, quiet: Vec<i32>) -> Vec<i32> { | ||
fn helper( | ||
person: usize, | ||
richer_map: &HashMap<usize, Vec<usize>>, | ||
quiet: &[i32], | ||
result: &mut [i32], | ||
) -> (i32, usize) { | ||
if result[person] != -1 { | ||
let person = result[person] as usize; | ||
return (quiet[person], person); | ||
} | ||
|
||
let mut min = (quiet[person], person); | ||
if let Some(richer) = richer_map.get(&person) { | ||
for &person in richer { | ||
let child = helper(person, richer_map, quiet, result); | ||
min = min.min(child); | ||
} | ||
} | ||
result[person] = min.1 as i32; | ||
min | ||
} | ||
|
||
let mut result = vec![-1; quiet.len()]; | ||
let mut richer_map = HashMap::<usize, Vec<usize>>::with_capacity(quiet.len()); | ||
for richer in richer { | ||
richer_map | ||
.entry(richer[1] as usize) | ||
.and_modify(|x| x.push(richer[0] as usize)) | ||
.or_insert_with(|| vec![richer[0] as usize]); | ||
} | ||
for person in 0..quiet.len() { | ||
helper(person, &richer_map, &quiet, &mut result); | ||
} | ||
result | ||
} | ||
} | ||
|
||
impl super::Solution for Solution { | ||
fn loud_and_rich(richer: Vec<Vec<i32>>, quiet: Vec<i32>) -> Vec<i32> { | ||
Self::loud_and_rich(richer, quiet) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
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,27 @@ | ||
pub mod dfs; | ||
|
||
pub trait Solution { | ||
fn loud_and_rich(richer: Vec<Vec<i32>>, quiet: Vec<i32>) -> Vec<i32>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [( | ||
( | ||
&[[1, 0], [2, 1], [3, 1], [3, 7], [4, 3], [5, 3], [6, 3]] as &[_], | ||
&[3, 2, 5, 4, 6, 1, 7, 0] as &[_], | ||
), | ||
&[5, 5, 2, 5, 4, 5, 6, 7] as &[_], | ||
)]; | ||
|
||
for ((richer, quiet), expected) in test_cases { | ||
assert_eq!( | ||
S::loud_and_rich(richer.iter().map(Vec::from).collect(), quiet.to_vec()), | ||
expected | ||
); | ||
} | ||
} | ||
} |