当前位置:网站首页>leetcode-2115:从给定原材料中找到所有可以做出的菜
leetcode-2115:从给定原材料中找到所有可以做出的菜
2022-07-02 23:55:00 【菊头蝙蝠】
题目
你有 n
道不同菜的信息。给你一个字符串数组 recipes
和一个二维字符串数组 ingredients
。第 i
道菜的名字为 recipes[i]
,如果你有它 所有 的原材料 ingredients[i]
,那么你可以 做出 这道菜。一道菜的原材料可能是 另一道 菜,也就是说 ingredients[i]
可能包含 recipes
中另一个字符串。
同时给你一个字符串数组 supplies
,它包含你初始时拥有的所有原材料,每一种原材料你都有无限多。
请你返回你可以做出的所有菜。你可以以 任意顺序 返回它们。
注意两道菜在它们的原材料中可能互相包含。
示例 1:
输入:recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = ["yeast","flour","corn"]
输出:["bread"]
解释:
我们可以做出 "bread" ,因为我们有原材料 "yeast" 和 "flour" 。
示例 2:
输入:recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread","meat"]], supplies = ["yeast","flour","meat"]
输出:["bread","sandwich"]
解释:
我们可以做出 "bread" ,因为我们有原材料 "yeast" 和 "flour" 。
我们可以做出 "sandwich" ,因为我们有原材料 "meat" 且可以做出原材料 "bread" 。
示例 3:
输入:recipes = ["bread","sandwich","burger"], ingredients = [["yeast","flour"],["bread","meat"],["sandwich","meat","bread"]], supplies = ["yeast","flour","meat"]
输出:["bread","sandwich","burger"]
解释:
我们可以做出 "bread" ,因为我们有原材料 "yeast" 和 "flour" 。
我们可以做出 "sandwich" ,因为我们有原材料 "meat" 且可以做出原材料 "bread" 。
我们可以做出 "burger" ,因为我们有原材料 "meat" 且可以做出原材料 "bread" 和 "sandwich" 。
示例 4:
输入:recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = ["yeast"]
输出:[]
解释:
我们没法做出任何菜,因为我们只有原材料 "yeast" 。
解题
方法一:暴力模拟
以现有的原料制作,把新做出来的菜,也添加到原料中,反复循环直到没有新的菜增加,就返回结果
class Solution {
public:
vector<string> findAllRecipes(vector<string>& recipes, vector<vector<string>>& ingredients, vector<string>& supplies) {
vector<string> res;
unordered_set<string> set;
for(string& s:supplies){
//记录现有的原材料
set.insert(s);
}
int preLen;//当前所有原材料的长度
while(true){
int preLen=set.size();
for(int i=0;i<recipes.size();i++){
if(set.count(recipes[i])) continue;
vector<string> tmp=ingredients[i];
bool ok=true;//如果为true说明可以做这道菜
for(int j=0;j<tmp.size();j++){
if(!set.count(tmp[j])){
ok=false;
break;
}
}
if(ok){
//如果本这次菜品可以做
set.insert(recipes[i]);//本次菜品添加为原材料
res.push_back(recipes[i]);//保存结果
}
}
if(set.size()<=preLen) break;//如果原材料不再增加了,说明剩下的菜品都没法制作了
}
return res;
}
};
方法二:拓扑排序
从原材料出发,当菜品对应的入度为0的时候,说明原材料齐全,把菜品再加入到原材料中。
得到入度为0的点,就是可以做的菜品。
class Solution {
public:
vector<string> findAllRecipes(vector<string>& recipes, vector<vector<string>>& ingredients, vector<string>& supplies) {
unordered_map<string,vector<string>> graph;
unordered_map<string,int> indeg;
int n=recipes.size();
for(int i=0;i<n;i++){
for(int j=0;j<ingredients[i].size();j++){
graph[ingredients[i][j]].push_back(recipes[i]);
indeg[recipes[i]]++;
}
}
queue<string> q;
for(string& s:supplies){
q.push(s);
}
vector<string> res;
while(!q.empty()){
string x=q.front();
q.pop();
for(string& y:graph[x]){
if(--indeg[y]==0){
q.push(y);
res.push_back(y);
}
}
}
return res;
}
};
边栏推荐
- Graduation summary
- Attributeerror: 'tuple' object has no attribute 'layer' problem solving
- redis21道经典面试题,极限拉扯面试官
- Centos7 one click compilation to build MySQL script
- 多进程编程(五):信号量
- 【JetCache】JetCache的配置说明和注解属性说明
- Some introduction and precautions about XML
- 【Pulsar文档】概念和架构/Concepts and Architecture
- Nc20806 District interval
- NC24840 [USACO 2009 Mar S]Look Up
猜你喜欢
图解网络:什么是虚拟路由器冗余协议 VRRP?
Redis21 classic interview questions, extreme pull interviewer
字符设备注册常用的两种方法和步骤
Automated defect analysis in electronic microscopic images
University of Toronto:Anthony Coache | 深度强化学习的条件可诱导动态风险度量
【单片机项目实训】八路抢答器
Multiprocess programming (I): basic concepts
利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度
Rust所有权(非常重要)
Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)
随机推荐
Use Jenkins II job
Solution to the problem of abnormal display of PDF exported Chinese documents of confluence
Rust字符串切片、结构体和枚举类
The most painful programming problem in 2021, adventure of code 2021 Day24
Shell implements basic file operations (cutting, sorting, and de duplication)
Nacos+openfeign error reporting solution
Free we media essential tools sharing
机器学习:numpy版本线性回归预测波士顿房价
NC24325 [USACO 2012 Mar S]Flowerpot
NC24840 [USACO 2009 Mar S]Look Up
【AutoSAR 七 工具链简介】
One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
Problèmes de configuration lex & yacc & Bison & Flex
【AutoSAR 一 概述】
Form form instantiation
Andorid gets the system title bar height
如何系统学习机器学习
Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
[MCU project training] eight way answering machine
1.12 - Instructions