Find All Possible Recipes from Given Supplies

Medium Topics Company Tags

You are given recipe names in recipes and a matching array ingredients, where ingredients[i] lists everything needed to make recipes[i]. Any recipe you make becomes available as an ingredient for other recipes.

The array supplies contains the ingredients available initially, in unlimited quantities. Making a recipe does not consume any available ingredient. Return all recipe names that can be made, in any order. Recipe dependencies may contain cycles.

Example 1:

Input: recipes = ["soup","dinner"]
ingredients = [["water","beans"],["soup","rice"]]
supplies = ["water","beans","rice"]

Output: ["dinner","soup"]

Explanation: Water and beans allow you to make soup. Once soup is available, combining it with rice allows you to make dinner. Both recipe names are returned.

Example 2:

Input: recipes = ["tea","toast"]
ingredients = [["water","leaves"],["bread"]]
supplies = ["water","leaves"]

Output: ["tea"]

Explanation: You have water and leaves, so tea can be made. Bread is unavailable and no recipe produces it, so toast cannot be made.

Example 3:

Input: recipes = ["alpha","beta"]
ingredients = [["beta"],["alpha"]]
supplies = ["salt"]

Output: []

Explanation: Alpha requires beta, and beta requires alpha. Neither is initially available, so this dependency cycle cannot be started.

Constraints:

  • 1 <= recipes.length == ingredients.length <= 100
  • 1 <= ingredients[i].length, supplies.length <= 100
  • Every name contains 1 to 10 lowercase English letters.
  • All names in recipes and supplies, combined, are unique.
  • Each ingredient list contains no duplicate names.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.