Find All Possible Recipes from Given Supplies - Problem

Imagine you're a master chef with a cookbook of recipes and a pantry full of supplies. Your challenge is to determine which recipes you can actually cook!

You have n different recipes, where each recipe has a name and requires specific ingredients. Here's the twist: some recipes can serve as ingredients for other recipes. For example, you might need "dough" to make "pizza", but "dough" itself is another recipe in your cookbook.

Given:

  • recipes[] - Array of recipe names
  • ingredients[][] - 2D array where ingredients[i] contains all ingredients needed for recipes[i]
  • supplies[] - Array of ingredients you initially have (infinite supply)

Goal: Return a list of all recipes you can make, considering that recipes can depend on other recipes. The order doesn't matter.

Note: Two recipes may contain each other in their ingredients (circular dependencies), but you should only return recipes that can actually be completed.

Input & Output

basic_recipes.py — Python
$ Input: recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = ["yeast","flour"]
Output: ["bread"]
💡 Note: We have yeast and flour, which are exactly the ingredients needed to make bread. Since we can make bread, it's included in the result.
recipe_dependencies.py — Python
$ Input: recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread","meat"]], supplies = ["yeast","flour","meat"]
Output: ["bread","sandwich"]
💡 Note: First we make bread using yeast and flour. Then we can make sandwich using the bread we just made plus meat from our supplies. Both recipes are possible.
impossible_recipe.py — Python
$ Input: recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread","meat"]], supplies = ["yeast"]
Output: []
💡 Note: We can't make bread because we're missing flour. Since bread can't be made, sandwich also can't be made (it needs bread as an ingredient). No recipes are possible.

Constraints

  • n == recipes.length == ingredients.length
  • 1 ≤ n ≤ 100
  • 1 ≤ ingredients[i].length ≤ 100
  • 1 ≤ supplies.length ≤ 100
  • 1 ≤ recipes[i].length, ingredients[i][j].length, supplies[k].length ≤ 10
  • All strings consist of only lowercase English letters
  • Each ingredients[i] does not contain duplicates

Visualization

Tap to expand
Recipe Dependency ResolutionInitial Supplies🌾 flour, 🧄 yeast🍞 breadReady!🥪 sandwichneeds: bread + meat🍕 pizzaneeds: bread + cheese🍔 burgerneeds: bread + meatProcessing Steps1. 🌾🧄 → 🍞 (Make bread first)2. 🍞 + 🥩 → 🥪 (Unlock sandwich)3. 🍞 + 🥩 → 🍔 (Unlock burger)❌ Pizza needs 🧀 (not available)
Understanding the Visualization
1
Map Dependencies
Create a graph showing which recipes depend on others
2
Find Starting Points
Identify recipes that can be made with current supplies
3
Process in Order
Make recipes and unlock new possibilities
4
Complete All Possible
Continue until no more recipes can be unlocked
Key Takeaway
🎯 Key Insight: Use topological sorting to handle recipe dependencies efficiently - process recipes in dependency order, unlocking new possibilities as you go!
Asked in
Amazon 35 Google 28 Meta 22 Microsoft 15
39.2K Views
Medium Frequency
~25 min Avg. Time
1.7K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen