Find All Possible Recipes from Given Supplies
Craft-game helper. You start with endless stocks of a few raw supplies, and each recipe turns its ingredients into one new item. An ingredient is a raw supply or the item another recipe makes, so recipes can build on each other. Two recipes that need each other, directly or through others, can never be made. Return the name of every recipe you can make, in any order. Details: recipes[i] needs every name in ingredients[i]. Once an item can be made you have as much of it as you like. A name may be listed twice in one recipe, and a recipe may be listed before the recipes it depends on. Limits: 1 <= recipes.length <= 50000, 1 <= supplies.length <= 50000, every recipe lists at least one ingredient, at most 200000 ingredient entries in all. Names are 1 to 10 lowercase letters or digits. Recipe names are distinct and never appear in supplies. A chain of recipes can be tens of thousands long, so avoid recursion. Example: supplies = [wood, iron, string] recipes = [bow, hammer, plank, nail, trap] ingredients = [[plank, string], [plank, nail], [wood], [iron], [gold, hammer]] => plank, nail, hammer, bow (in any order) plank and nail come straight from the supplies, then hammer and bow follow. trap needs gold, which is neither a supply nor a recipe. The stub returns a placeholder name, so an untouched stub never passes.
You've got a few endless supplies and a list of recipes, and some recipes call for other recipes as ingredients. Which dishes can you actually make? Interviewers like it because it looks like a tangle until you spot the dependency graph, and the classic trap is looping forever when two recipes need each other. Expect up to 50,000 recipes, 200,000 ingredient entries in all, and names of 1 to 10 lowercase letters or digits.
Examples
You write
find_all_recipes(recipes: List[str], ingredients: List[List[str]], supplies: List[str])
Example 1
- Input
- recipes = ["bow","hammer","plank","nail","trap"], ingredients = [["plank","string"],["plank","nail"],["wood"],["iron"],["gold","hammer"]], supplies = ["wood","iron","string"]
- returns
- ["plank","nail","hammer","bow"]
Example 2
- Input
- recipes = ["soup"], ingredients = [["water","salt"]], supplies = ["water","salt"]
- returns
- ["soup"]
Save this for your laptop
The editor and Run need a bigger screen. The link opens Find All Possible Recipes from Given Supplies ready to code. It is free, and the editor needs no account.
The pattern behind it
Graphs
I reach for it when
- The input is edges, prerequisites or connections.
- You need cycles, an ordering or connected groups.
Say this before you type. Say whether the edges are directed, and which of the three jobs you are doing.
All 32 Graphs problemsArrays & Hashing
I reach for it when
- You catch yourself scanning the array again for every element.
- The problem asks about duplicates, pairs, counts or groups.
Say this before you type. Say what the key is and what the value remembers, before you write the loop.
All 126 Arrays & Hashing problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit, and to talk with her for one hour. After that, talking comes with the $79 pack. Anyone can open the editor and run their code.