Skip to main content
Back to problems
Leetcode
Medium
Graphs
Hash Maps
Sets
Queues
Google
Find All Possible Recipes From Given Supplies

Determine which recipes can be prepared from an initial set of supplies and other recipes that become available later.

Acceptance 0%
Problem Statement

You are given a list of recipe names, a list of ingredient lists, and a list of starting supplies.

A recipe can be prepared if every ingredient in its list is already available. An ingredient is available if it is either one of the initial supplies or a recipe that has already been prepared.

Return all recipes that can eventually be made. You may use the prepared recipes as ingredients for other recipes, and recipes can depend on one another. If a dependency cycle prevents a recipe from ever being made, it should not appear in the answer.

The order of the returned recipes does not matter, as long as every makeable recipe is included exactly once.

Input Format

  • recipes: list of recipe names
  • ingredients: ingredients[i] lists the ingredients needed for recipes[i]
  • supplies: list of initially available items

Each recipe name is unique.

Output Format

Return a list of all recipes that can be prepared using the initial supplies and any recipes that become available later.

Constraints

  • 1 <= recipes.length == ingredients.length <= 100
  • Recipe names and ingredient names are non-empty strings
  • Ingredient names may refer to either supplies or other recipes
  • Answer order does not matter
Examples
Sample cases returned by the problem API.

Example 1

Input

recipes = ["bread","sandwich","burger"], ingredients = [["yeast","flour"],["bread","meat"],["sandwich","bread"]], supplies = ["yeast","flour","meat"]

Output

["bread","sandwich","burger"]

Explanation

Bread can be made first from the supplies. Then sandwich can be made using bread and meat. Finally burger can be made using sandwich and bread.

Example 2

Input

recipes = ["cake","icing"], ingredients = [["flour","sugar"],["cake","sugar"]], supplies = ["flour","sugar"]

Output

["cake","icing"]

Explanation

Cake is makeable from the supplies. Once cake is available, icing becomes makeable as well.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.