跳到主要内容

2115.从给定原材料中找到所有可以做出的菜

链接:2115.从给定原材料中找到所有可以做出的菜
难度:Medium
标签:图、拓扑排序、数组、哈希表、字符串
简介:你有 n 道不同菜的信息。请你返回你可以做出的所有菜。

题解 1 - cpp

  • 编辑时间:2021-12-31
  • 执行用时:1380ms
  • 内存消耗:91.1MB
  • 编程语言:cpp
  • 解法介绍:循环遍历,然后判断每个菜能否被做出。
class Solution {
public:
vector<string> findAllRecipes(vector<string>& recipes,
vector<vector<string>>& ingredients,
vector<string>& supplies) {
unordered_set<string> supply_set, recipe_set;
for (auto& supply : supplies) supply_set.insert(supply);
for (int i = 0; i < recipes.size(); i++) {
for (int i = 0; i < recipes.size(); i++) {
if (recipe_set.count(recipes[i])) continue;
int tag = 1;
for (auto& ingredient : ingredients[i]) {
if (supply_set.count(ingredient) ||
recipe_set.count(ingredient))
continue;
tag = 0;
break;
}
if (tag) recipe_set.insert(recipes[i]);
}
}
vector<string> ans;
for (auto it = recipe_set.begin(); it != recipe_set.end(); it++)
ans.push_back(*it);
return ans;
}
};