当前位置:网站首页>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
}
}
边栏推荐
- Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 2 --blinker_ Hello_ WiFi (lighting technology - Mobile App control routine)
- Desktop icon recognition based on OpenCV
- Solve the problem of disordered code in vscode development, output Chinese and open source code
- Filter comments to filter out uncommented and default values
- LeetCode每日一题(985. Sum of Even Numbers After Queries)
- Crawler career from scratch (3): crawl the photos of my little sister ③ (the website has been disabled)
- Spark 集群安装与部署
- Spark cluster installation and deployment
- [graduation season | advanced technology Er] another graduation season, I change my career as soon as I graduate, from animal science to programmer. Programmers have something to say in 10 years
- Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)
猜你喜欢

Principles of computer composition - cache, connection mapping, learning experience

Trial of the combination of RDS and crawler

IDEA 中使用 Hudi

Detailed steps of windows installation redis

【点云处理之论文狂读前沿版11】—— Unsupervised Point Cloud Pre-training via Occlusion Completion

Solve POM in idea Comment top line problem in XML file

全球KYC服务商ADVANCE.AI 活体检测产品通过ISO国际安全认证 产品能力再上一新台阶

PolyWorks script development learning notes (III) -treeview advanced operation
![[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu](/img/57/b413a93a456a1872fc19aa825c937a.jpg)
[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu

PolyWorks script development learning notes (I) - script development environment
随机推荐
Move anaconda, pycharm and jupyter notebook to mobile hard disk
PolyWorks script development learning notes (I) - script development environment
Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)
Serializer rewrite: update and create methods
一款开源的Markdown转富文本编辑器的实现原理剖析
ERROR: certificate common name “*.” doesn’t match requested ho
Please tell me how to set vscode
Construction of simple database learning environment
Flink学习笔记(八)多流转换
Beego learning - JWT realizes user login and registration
Jestson Nano自定义根文件系统创建(支持NVIDIA图形库的最小根文件系统)
npm install安装依赖包报错解决方法
Windows安装Redis详细步骤
Hudi 快速体验使用(含操作详细步骤及截图)
Crawler career from scratch (V): detailed explanation of re regular expression
[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals
Go language - Reflection
Spark structured stream writing Hudi practice
Solve the problem of disordered code in vscode development, output Chinese and open source code
Spark 集群安装与部署