当前位置:网站首页>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
}
}
边栏推荐
- Flink-CDC实践(含实操步骤与截图)
- Jestson Nano 从tftp服务器下载更新kernel和dtb
- Win10 install elk
- npm install安装依赖包报错解决方法
- Arduino handles JSON data, arduinojson assistant
- Linxu learning (4) -- Yum and apt commands
- ERROR: certificate common name “*.” doesn’t match requested ho
- Modify idea code
- LeetCode每日一题(931. Minimum Falling Path Sum)
- Win10 quick screenshot
猜你喜欢

Crawler career from scratch (3): crawl the photos of my little sister ③ (the website has been disabled)

Solve the problem of disordered code in vscode development, output Chinese and open source code

Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)

LeetCode每日一题(2090. K Radius Subarray Averages)

Spark 结构化流写入Hudi 实践

Hudi integrated spark data analysis example (including code flow and test results)

Modify idea code

图像修复方法研究综述----论文笔记

NPM install installation dependency package error reporting solution

Flink学习笔记(八)多流转换
随机推荐
【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定
WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available. Prompt to upgrade pip
[kotlin learning] control flow of higher-order functions -- lambda return statements and anonymous functions
unbuntu(debian)下TFTP服务器搭建及测试
ERROR: certificate common name “*.” doesn’t match requested ho
Make the most basic root file system of Jetson nano and mount NFS file system on the server
文件系统中的目录与切换操作
Jenkins learning (I) -- Jenkins installation
Flask+supervisor installation realizes background process resident
Hudi integrated spark data analysis example (including code flow and test results)
NPM install installation dependency package error reporting solution
[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion
[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals
【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
Learning C language from scratch -- installation and configuration of 01 MinGW
软件测试工程师是做什么的 通过技术测试软件程序中是否有漏洞
Crawler career from scratch (IV): climb the bullet curtain of station B through API
ERROR: certificate common name “www.mysql.com” doesn’t match requested host name “137.254.60.11”.
[kotlin learning] classes, objects and interfaces - define class inheritance structure
【Kotlin学习】类、对象和接口——定义类继承结构