当前位置:网站首页>Find all possible recipes from given supplies
Find all possible recipes from given supplies
2022-07-03 09:33:00 【wangjun861205】
You have information about n different recipes. You are given a string array recipes and a 2D string array ingredients. The ith recipe has the name recipes[i], and you can create it if you have all the needed ingredients from ingredients[i]. Ingredients to a recipe may need to be created from other recipes, i.e., ingredients[i] may contain a string that is in recipes.
You are also given a string array supplies containing all the ingredients that you initially have, and you have an infinite supply of all of them.
Return a list of all the recipes that you can create. You may return the answer in any order.
Note that two recipes may contain each other in their ingredients.
Example 1:
Input: recipes = [“bread”], ingredients = [[“yeast”,“flour”]], supplies = [“yeast”,“flour”,“corn”]
Output: [“bread”]
Explanation:
We can create “bread” since we have the ingredients “yeast” and “flour”.
Example 2:
Input: recipes = [“bread”,“sandwich”], ingredients = [[“yeast”,“flour”],[“bread”,“meat”]], supplies = [“yeast”,“flour”,“meat”]
Output: [“bread”,“sandwich”]
Explanation:
We can create “bread” since we have the ingredients “yeast” and “flour”.
We can create “sandwich” since we have the ingredient “meat” and can create the ingredient “bread”.
Example 3:
Input: recipes = [“bread”,“sandwich”,“burger”], ingredients = [[“yeast”,“flour”],[“bread”,“meat”],[“sandwich”,“meat”,“bread”]], supplies = [“yeast”,“flour”,“meat”]
Output: [“bread”,“sandwich”,“burger”]
Explanation:
We can create “bread” since we have the ingredients “yeast” and “flour”.
We can create “sandwich” since we have the ingredient “meat” and can create the ingredient “bread”.
We can create “burger” since we have the ingredient “meat” and can create the ingredients “bread” and “sandwich”.
Constraints:
- n == recipes.length == ingredients.length
- 1 <= n <= 100
- 1 <= ingredients[i].length, supplies.length <= 100
- 1 <= recipes[i].length, ingredients[i][j].length, supplies[k].length <= 10
- recipes[i], ingredients[i][j], and supplies[k] consist only of lowercase English letters.
- All the values of recipes and supplies combined are unique.
- Each ingredients[i] does not contain any duplicate values.
There are actually dead cycles in the recipe , I want to make bread , Need flour and cream , But flour needs bread to make , It doesn't feel like a recipe , More like economics , We provide bread for workers to produce flour , It should mean .
Put aside these illogical questions , In fact, this is a path finding problem , Let's supplies As a collection of all leaf nodes , As long as a recipe All of the ingredients All in supplies In the , We thought we could make this kind of food . If recipe Some kind of ingredient Not in supplies in , There are two possibilities , One is to ingredient It's also a recipe, And we haven't checked whether it can be done . Another situation is that we really don't have this kind of raw material , We can't make this food .
use std::collections::{
HashMap, HashSet};
impl Solution {
fn find(
recipes: &HashMap<String, HashSet<String>>,
key: &str,
cache: &mut HashMap<String, bool>,
mut visited: HashSet<String>,
) -> bool {
visited.insert(key.to_owned());
if let Some(ok) = cache.get(key) {
return *ok;
}
if let Some(igds) = recipes.get(key) {
for igd in igds {
if visited.contains(igd) {
return false;
}
if let Some(igd) = cache.get(igd) {
if !(*igd) {
cache.insert(key.to_owned(), false);
return false;
}
continue;
}
if !Solution::find(recipes, igd, cache, visited.clone()) {
cache.insert(key.to_owned(), false);
return false;
}
}
cache.insert(key.to_owned(), true);
return true;
}
cache.insert(key.to_owned(), false);
false
}
pub fn find_all_recipes(
recipes: Vec<String>,
ingredients: Vec<Vec<String>>,
supplies: Vec<String>,
) -> Vec<String> {
let mut rc = HashMap::new();
for (r, igds) in recipes.clone().into_iter().zip(ingredients) {
let set: HashSet<String> = igds.into_iter().collect();
rc.insert(r, set);
}
let mut cache: HashMap<String, bool> = supplies.into_iter().map(|v| (v, true)).collect();
let mut ans = Vec::new();
for r in recipes {
if Solution::find(&rc, &r, &mut cache, HashSet::new()) {
ans.push(r);
}
}
ans
}
}
边栏推荐
- Arduino handles JSON data, arduinojson assistant
- Please tell me how to set vscode
- Go language - Reflection
- [point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks
- Temper cattle ranking problem
- Detailed steps of windows installation redis
- The rise and fall of mobile phones in my perspective these 10 years
- Banner - Summary of closed group meeting
- 用Redis实现分布式锁
- [set theory] order relation (chain | anti chain | chain and anti chain example | chain and anti chain theorem | chain and anti chain inference | good order relation)
猜你喜欢
Flask+supervisor installation realizes background process resident
Arduino handles JSON data, arduinojson assistant
[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion
Hudi integrated spark data analysis example (including code flow and test results)
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
Temper cattle ranking problem
【毕业季|进击的技术er】又到一年毕业季,一毕业就转行,从动物科学到程序员,10年程序员有话说
Common software open source protocols
Uncle Wang's blog directory [constantly updating]
Flink学习笔记(八)多流转换
随机推荐
There is no open in default browser option in the right click of the vscade editor
2022-2-14 learning the imitation Niuke project - send email
Spark 概述
Flask+supervisor installation realizes background process resident
Modify idea code
WARNING: You are using pip ; however. Later, upgrade PIP failed, modulenotfounderror: no module named 'pip‘
数字身份验证服务商ADVANCE.AI顺利加入深跨协 推进跨境电商行业可持续性发展
Hudi学习笔记(三) 核心概念剖析
专利查询网站
[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks
Hudi learning notes (III) analysis of core concepts
软件测试工程师是做什么的 通过技术测试软件程序中是否有漏洞
LeetCode每日一题(2305. Fair Distribution of Cookies)
IDEA 中使用 Hudi
Crawler career from scratch (3): crawl the photos of my little sister ③ (the website has been disabled)
Desktop icon recognition based on OpenCV
1922. Count Good Numbers
ERROR: certificate common name “*.” doesn’t match requested ho
Windows安装Redis详细步骤
Go language - JSON processing