-
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 2115: Find All Possible Recipes from Given Supplies
- Loading branch information
Showing
3 changed files
with
166 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
95 changes: 95 additions & 0 deletions
95
src/problem_2115_find_all_possible_recipes_from_given_supplies/bfs.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,95 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::collections::hash_map::Entry; | ||
use std::collections::{HashMap, VecDeque}; | ||
use std::{iter, mem}; | ||
|
||
impl Solution { | ||
fn intern<'a>(word_to_id: &mut HashMap<&'a str, u16>, word: &'a str) -> u16 { | ||
let candidate = word_to_id.len() as _; | ||
|
||
match word_to_id.entry(word) { | ||
Entry::Occupied(entry) => *entry.get(), | ||
Entry::Vacant(entry) => { | ||
entry.insert(candidate); | ||
|
||
candidate | ||
} | ||
} | ||
} | ||
|
||
pub fn find_all_recipes(recipes: Vec<String>, ingredients: Vec<Vec<String>>, supplies: Vec<String>) -> Vec<String> { | ||
let mut word_to_id = (0..).zip(&recipes).map(|(id, recipe)| (recipe.as_str(), id)).collect(); | ||
|
||
let mut in_degrees = ingredients | ||
.iter() | ||
.map(|ingredients| ingredients.len() as u8) | ||
.collect::<Box<_>>(); | ||
|
||
let mut graph = Vec::<Vec<u16>>::new(); | ||
|
||
for (recipe, ingredients) in (0..).zip(&ingredients) { | ||
for ingredient in ingredients { | ||
let ingredient = usize::from(Self::intern(&mut word_to_id, ingredient)); | ||
|
||
if let Some(nexts) = graph.get_mut(ingredient) { | ||
nexts.push(recipe); | ||
} else { | ||
let extra = ingredient - graph.len(); | ||
|
||
graph.extend(iter::repeat_with(Vec::new).take(extra)); | ||
graph.push(vec![recipe]); | ||
} | ||
} | ||
} | ||
|
||
let mut result = Vec::new(); | ||
|
||
let mut queue = supplies | ||
.iter() | ||
.filter_map(|supply| word_to_id.get(supply.as_str()).copied()) | ||
.collect::<VecDeque<_>>(); | ||
|
||
drop(word_to_id); | ||
|
||
let mut recipes = recipes; | ||
|
||
while !queue.is_empty() { | ||
for _ in 0..queue.len() { | ||
let ingredient = usize::from(queue.pop_front().unwrap()); | ||
|
||
for &recipe in &graph[ingredient] { | ||
let recipe = usize::from(recipe); | ||
let in_degree = &mut in_degrees[recipe]; | ||
|
||
if *in_degree == 1 { | ||
result.push(mem::take(&mut recipes[recipe])); | ||
queue.push_back(recipe as _); | ||
} else { | ||
*in_degree -= 1; | ||
} | ||
} | ||
} | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn find_all_recipes(recipes: Vec<String>, ingredients: Vec<Vec<String>>, supplies: Vec<String>) -> Vec<String> { | ||
Self::find_all_recipes(recipes, ingredients, supplies) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/problem_2115_find_all_possible_recipes_from_given_supplies/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,70 @@ | ||
pub mod bfs; | ||
|
||
pub trait Solution { | ||
fn find_all_recipes(recipes: Vec<String>, ingredients: Vec<Vec<String>>, supplies: Vec<String>) -> Vec<String>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
use crate::test_utilities; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
( | ||
( | ||
&["bread"] as &[_], | ||
&[&["yeast", "flour"] as &[_]] as &[&[_]], | ||
&["yeast", "flour", "corn"] as &[_], | ||
), | ||
&["bread"] as &[_], | ||
), | ||
( | ||
( | ||
&["bread", "sandwich"], | ||
&[&["yeast", "flour"], &["bread", "meat"]], | ||
&["yeast", "flour", "meat"], | ||
), | ||
&["bread", "sandwich"], | ||
), | ||
( | ||
( | ||
&["bread", "sandwich", "burger"], | ||
&[&["yeast", "flour"], &["bread", "meat"], &["sandwich", "meat", "bread"]], | ||
&["yeast", "flour", "meat"], | ||
), | ||
&["bread", "burger", "sandwich"], | ||
), | ||
( | ||
( | ||
&["ju", "fzjnm", "x", "e", "zpmcz", "h", "q"], | ||
&[ | ||
&["d"], | ||
&["hveml", "f", "cpivl"], | ||
&["cpivl", "zpmcz", "h", "e", "fzjnm", "ju"], | ||
&["cpivl", "hveml", "zpmcz", "ju", "h"], | ||
&["h", "fzjnm", "e", "q", "x"], | ||
&["d", "hveml", "cpivl", "q", "zpmcz", "ju", "e", "x"], | ||
&["f", "hveml", "cpivl"], | ||
], | ||
&["f", "hveml", "cpivl", "d"], | ||
), | ||
&["fzjnm", "ju", "q"], | ||
), | ||
]; | ||
|
||
for ((recipes, ingredients, supplies), expected) in test_cases { | ||
assert_eq!( | ||
test_utilities::unstable_sorted(S::find_all_recipes( | ||
recipes.iter().copied().map(str::to_string).collect(), | ||
ingredients | ||
.iter() | ||
.map(|ingredients| ingredients.iter().copied().map(str::to_string).collect()) | ||
.collect(), | ||
supplies.iter().copied().map(str::to_string).collect(), | ||
)), | ||
expected, | ||
); | ||
} | ||
} | ||
} |