2115. Find All Possible Recipes from Given Supplies
Question
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]. A recipe can also be an ingredient for 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"|"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"|"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.length1 <= n <= 1001 <= ingredients[i].length, supplies.length <= 1001 <= recipes[i].length, ingredients[i][j].length, supplies[k].length <= 10recipes[i], ingredients[i][j], andsupplies[k]consist only of lowercase English letters.- All the values of
recipesandsuppliescombined are unique. - Each
ingredients[i]does not contain any duplicate values.
Approach 1: BFS
Intuition
Algorithm
Code
class Solution {
public:
vector<string> findAllRecipes(vector<string>& recipes,
vector<vector<string>>& ingredients,
vector<string>& supplies) {
// Track available ingredients and recipes
unordered_set<string> available(supplies.begin(), supplies.end());
// Queue to process recipe indices
queue<int> recipeQueue;
for (int idx = 0; idx < recipes.size(); ++idx) {
recipeQueue.push(idx);
}
vector<string> createdRecipes;
int lastSize = -1;
// Continue while we keep finding new recipes
while (static_cast<int>(available.size()) > lastSize) {
lastSize = available.size();
int queueSize = recipeQueue.size();
// Process all recipes in current queue
while (queueSize-- > 0) {
int recipeIdx = recipeQueue.front();
recipeQueue.pop();
bool canCreate = true;
// Check if all ingredients are available
for (string& ingredient : ingredients[recipeIdx]) {
if (!available.count(ingredient)) {
canCreate = false;
break;
}
}
if (!canCreate) {
recipeQueue.push(recipeIdx);
} else {
// Recipe can be created - add to available items
available.insert(recipes[recipeIdx]);
createdRecipes.push_back(recipes[recipeIdx]);
}
}
}
return createdRecipes;
}
};
Complexity Analysis
- Time Complexity:
- Space Complexity:
Approach 2: Topo sort Kahn's Algorithm
Intuition
Algorithm
Code
class Solution {
public:
vector<string> findAllRecipes(vector<string>& recipes,
vector<vector<string>>& ingredients,
vector<string>& supplies) {
// Track available ingredients and recipes
unordered_set<string> available(supplies.begin(), supplies.end());
// Queue to process recipe indices
queue<int> recipeQueue;
for (int idx = 0; idx < recipes.size(); ++idx) {
recipeQueue.push(idx);
}
vector<string> createdRecipes;
int lastSize = -1;
// Continue while we keep finding new recipes
while (static_cast<int>(available.size()) > lastSize) {
lastSize = available.size();
int queueSize = recipeQueue.size();
// Process all recipes in current queue
while (queueSize-- > 0) {
int recipeIdx = recipeQueue.front();
recipeQueue.pop();
bool canCreate = true;
// Check if all ingredients are available
for (string& ingredient : ingredients[recipeIdx]) {
if (!available.count(ingredient)) {
canCreate = false;
break;
}
}
if (!canCreate) {
recipeQueue.push(recipeIdx);
} else {
// Recipe can be created - add to available items
available.insert(recipes[recipeIdx]);
createdRecipes.push_back(recipes[recipeIdx]);
}
}
}
return createdRecipes;
}
};
Complexity Analysis
- Time Complexity:
- Space Complexity:
Approach 3: DFS
Intuition
Algorithm
Code
Complexity Analysis
- Time Complexity:
- Space Complexity: