Published: Jan 20, 2023
Problem Description
You have information about
n
different recipes. You are given a string arrayrecipes
and a 2D string arrayingredients
. The i-th recipe has the namerecipes[i]
, and you can create it if you have all the needed ingredients fromingredients[i]
. Ingredients to a recipe may need to be created from other recipes, i.e.,ingredients[i]
may contain a string that is inrecipes
.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.
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
andsupplies
combined are unique.- Each
ingredients[i]
does not contain any duplicate values.https://leetcode.com/problems/find-all-possible-recipes-from-given-supplies/
Examples
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".
Exmaple 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".
How to Solve
The topological sort is a good approach for this problem. A graph is created by mapping ingredients to recipes. While creating a graph, count indegrees of each recipes. Start graph traversal from the indegree 0 nodes. To check visited nodes, decrement an indegree count. When the indegree count becomes 0, the node should be added to the queue. In the end, find the node whose indegree is 0 and the name is one of recipes.
Solution
class FindAllPossibleRecipesFromGivenSupplies {
public:
vector<string> findAllRecipes(vector<string>& recipes, vector<vector<string>>& ingredients, vector<string>& supplies) {
unordered_map<string, set<string>> graph;
unordered_map<string, int> indegree;
for (size_t i = 0; i < recipes.size(); ++i) {
for (string &parent : ingredients[i]) {
graph[parent].insert(recipes[i]);
indegree[recipes[i]]++;
}
}
queue<string> q;
for (auto &sup : supplies) {
if (indegree[sup] == 0) {
q.push(sup);
}
}
while (!q.empty()) {
string cur = q.front();
q.pop();
for (auto &neigh : graph[cur]) {
indegree[neigh]--;
if (indegree[neigh] == 0) { q.push(neigh); }
}
}
vector<string> result;
for (auto &[key, value] : indegree) {
if (value == 0 && find(recipes.begin(), recipes.end(), key) != recipes.end()) {
result.push_back(key);
}
}
return result;
}
};
class FindAllPossibleRecipesFromGivenSupplies:
def findAllRecipes(self, recipes: List[str], ingredients: List[List[str]], supplies: List[str]) -> List[str]:
graph = defaultdict(set)
indegree = defaultdict(int)
for child, parents in zip(recipes, ingredients):
for p in parents:
graph[p].add(child)
indegree[child] += 1
queue = []
for sup in supplies:
if indegree[sup] == 0:
queue.append(sup)
while queue:
cur = queue.pop(0)
for neigh in graph[cur]:
indegree[neigh] -= 1
if indegree[neigh] == 0:
queue.append(neigh)
return [k for k, v in indegree.items() if v == 0 and k in recipes]
Complexities
- Time:
O(m * n)
- Space:
O(n)