Skip to content

Commit f2dc0a2

Browse files
committed
@Solution: Find All Possible Recipes from Given Supplies
1 parent 9166fe4 commit f2dc0a2

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Find All Possible Recipes from Given Supplies
2+
3+
You have information about n different recipes. You are given a string array recipes and a 2D string array ingredients. The ith recipe has the name recipes[i], and you can create it if you have all the needed ingredients from ingredients[i]. A recipe can also be an ingredient for other recipes, i.e., ingredients[i] may contain a string that is in recipes.
4+
5+
You are also given a string array supplies containing all the ingredients that you initially have, and you have an infinite supply of all of them.
6+
7+
Return a list of all the recipes that you can create. You may return the answer in any order.
8+
9+
Note that two recipes may contain each other in their ingredients.
10+
11+
12+
Example 1:
13+
14+
```
15+
Input: recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = ["yeast","flour","corn"]
16+
Output: ["bread"]
17+
Explanation:
18+
We can create "bread" since we have the ingredients "yeast" and "flour".
19+
```
20+
21+
Example 2:
22+
23+
```
24+
Input: recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread","meat"]], supplies = ["yeast","flour","meat"]
25+
Output: ["bread","sandwich"]
26+
Explanation:
27+
We can create "bread" since we have the ingredients "yeast" and "flour".
28+
We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
29+
```
30+
Example 3:
31+
32+
```
33+
Input: recipes = ["bread","sandwich","burger"], ingredients = [["yeast","flour"],["bread","meat"],["sandwich","meat","bread"]], supplies = ["yeast","flour","meat"]
34+
Output: ["bread","sandwich","burger"]
35+
Explanation:
36+
We can create "bread" since we have the ingredients "yeast" and "flour".
37+
We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
38+
We can create "burger" since we have the ingredient "meat" and can create the ingredients "bread" and "sandwich".
39+
```
40+
41+
**Constraints:**
42+
- n == recipes.length == ingredients.length
43+
- 1 <= n <= 100
44+
- 1 <= ingredients[i].length, supplies.length <= 100
45+
- 1 <= recipes[i].length, ingredients[i][j].length, supplies[k].length <= 10
46+
- recipes[i], ingredients[i][j], and supplies[k] consist only of lowercase English letters.
47+
- All the values of recipes and supplies combined are unique.
48+
- Each ingredients[i] does not contain any duplicate values.
49+
50+
[Link](https://leetcode.com/problems/find-all-possible-recipes-from-given-supplies/description)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def findAllRecipes(self, recipes: List[str], ingredients: List[List[str]], supplies: List[str]) -> List[str]:
6+
recipe_to_ingredients = {recipe: set(ingr) for recipe, ingr in zip(recipes, ingredients)}
7+
available_supplies = set(supplies)
8+
result: List[str] = []
9+
recipes_to_check = set(recipes)
10+
11+
def can_cook(recipe: str) -> bool:
12+
for ingredient in recipe_to_ingredients[recipe]:
13+
if ingredient not in available_supplies:
14+
return False
15+
return True
16+
17+
while recipes_to_check:
18+
removable_recipes = []
19+
20+
for recipe in recipes_to_check:
21+
if can_cook(recipe):
22+
result.append(recipe)
23+
available_supplies.add(recipe)
24+
removable_recipes.append(recipe)
25+
26+
for recipe in removable_recipes:
27+
recipes_to_check.remove(recipe)
28+
29+
if not removable_recipes:
30+
break
31+
32+
return result
33+
34+
35+
s = Solution()
36+
print(s.findAllRecipes(recipes=["bread"], ingredients=[["yeast", "flour"]], supplies=["yeast", "flour", "corn"]))
37+
print(s.findAllRecipes(recipes=["bread", "sandwich"], ingredients=[["yeast", "flour"], ["bread", "meat"]],
38+
supplies=["yeast", "flour", "meat"]))
39+
print(s.findAllRecipes(recipes=["bread", "sandwich", "burger"],
40+
ingredients=[["yeast", "flour"], ["bread", "meat"], ["sandwich", "meat", "bread"]],
41+
supplies=["yeast", "flour", "meat"]))

0 commit comments

Comments
 (0)