bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 2115. Find All Possible Recipes from Given Supplies

2115. Find All Possible Recipes from Given Supplies

Leetcode Solutions

Topological Sort Approach

  1. Initialize a graph (adjacency list) and an indegree map to keep track of the number of prerequisites for each recipe.
  2. For each recipe, set its indegree to 0 in the indegree map.
  3. 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.
  4. Initialize a queue and add all supplies to it (since they have an indegree of 0).
  5. 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.
  6. Return the result list containing all recipes that can be created.
UML Thumbnail

Brute Force Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR