当前位置:网站首页>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;
}
};
边栏推荐
- 【AutoSAR 八 OS】
- 图解网络:什么是虚拟路由器冗余协议 VRRP?
- 【AutoSAR 六 描述文件】
- Markdown tutorial
- Form form instantiation
- Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
- 多进程编程(三):消息队列
- 2022上半年值得被看见的10条文案,每一句都能带给你力量!
- 关于QByteArray存储十六进制 与十六进制互转
- How to find out the currently running version of Solr- How do I find out version of currently running Solr?
猜你喜欢
多进程编程(一):基本概念
Solution to the problem of abnormal display of PDF exported Chinese documents of confluence
Attributeerror: 'tuple' object has no attribute 'layer' problem solving
One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size
Two common methods and steps of character device registration
【AutoSAR 四 BSW概述】
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail
pageoffice-之bug修改之旅
随机推荐
【AutoSAR 八 OS】
Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
Shell 实现文件基本操作(切割、排序、去重)
[pulsar document] concepts and architecture
Pageoffice - bug modification journey
Explain in detail the significance of the contour topology matrix obtained by using the contour detection function findcontours() of OpenCV, and how to draw the contour topology map with the contour t
lex && yacc && bison && flex 配置的問題
多进程编程(一):基本概念
微信小程序获取某个元素的信息(高、宽等),并将px转换为rpx。
详解用OpenCV的轮廓检测函数findContours()得到的轮廓拓扑结构(hiararchy)矩阵的意义、以及怎样用轮廓拓扑结构矩阵绘制轮廓拓扑结构图
利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度
Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
Vulkan并非“灵药“
Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size
Array common operation methods sorting (including ES6) and detailed use
Markdown使用教程
University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
JSON转换工具类
kubernetes编写yml简单入门
Use Jenkins II job