Skip to content

Commit ff7fab3

Browse files
committed
find alll
1 parent 93afb75 commit ff7fab3

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
"""
3+
2115. Find All Possible Recipes from Given Supplies
4+
5+
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.
6+
7+
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.
8+
9+
Return a list of all the recipes that you can create. You may return the answer in any order.
10+
11+
Note that two recipes may contain each other in their ingredients.
12+
13+
14+
15+
Example 1:
16+
17+
Input: recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = ["yeast","flour","corn"]
18+
Output: ["bread"]
19+
Explanation:
20+
We can create "bread" since we have the ingredients "yeast" and "flour".
21+
Example 2:
22+
23+
Input: recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread","meat"]], supplies = ["yeast","flour","meat"]
24+
Output: ["bread","sandwich"]
25+
Explanation:
26+
We can create "bread" since we have the ingredients "yeast" and "flour".
27+
We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
28+
Example 3:
29+
30+
Input: recipes = ["bread","sandwich","burger"], ingredients = [["yeast","flour"],["bread","meat"],["sandwich","meat","bread"]], supplies = ["yeast","flour","meat"]
31+
Output: ["bread","sandwich","burger"]
32+
Explanation:
33+
We can create "bread" since we have the ingredients "yeast" and "flour".
34+
We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
35+
We can create "burger" since we have the ingredient "meat" and can create the ingredients "bread" and "sandwich".
36+
37+
38+
Constraints:
39+
40+
n == recipes.length == ingredients.length
41+
1 <= n <= 100
42+
1 <= ingredients[i].length, supplies.length <= 100
43+
1 <= recipes[i].length, ingredients[i][j].length, supplies[k].length <= 10
44+
recipes[i], ingredients[i][j], and supplies[k] consist only of lowercase English letters.
45+
All the values of recipes and supplies combined are unique.
46+
Each ingredients[i] does not contain any duplicate values.
47+
"""
48+
49+
class Solution:
50+
def findAllRecipes(
51+
self, recipes: List[str], ingredients: List[List[str]], supplies: List[str]
52+
) -> List[str]:
53+
g = defaultdict(list)
54+
indeg = defaultdict(int)
55+
for a, b in zip(recipes, ingredients):
56+
for v in b:
57+
g[v].append(a)
58+
indeg[a] += len(b)
59+
q = supplies
60+
ans = []
61+
for i in q:
62+
for j in g[i]:
63+
indeg[j] -= 1
64+
if indeg[j] == 0:
65+
ans.append(j)
66+
q.append(j)
67+
return ans

0 commit comments

Comments
 (0)