Leetcode Problem 2115. Find All Possible Recipes from Given Supplies
2115. Find All Possible Recipes from Given Supplies
Leetcode Solutions
Topological Sort Approach
Initialize a graph (adjacency list) and an indegree map to keep track of the number of prerequisites for each recipe.
For each recipe, set its indegree to 0 in the indegree map.
For each ingredient in each recipe, if it is not a supply, add an edge from the ingredient to the recipe and increment the recipe's indegree.
Initialize a queue and add all supplies to it (since they have an indegree of 0).
While the queue is not empty:
a. Dequeue an ingredient.
b. For each recipe that the ingredient can make (from the graph), decrement its indegree.
c. If a recipe's indegree becomes 0, add it to the queue and to the result list.
Return the result list containing all recipes that can be created.