Find All Possible Recipes from Given Supplies LeetCode solution in Python

1
class Solution:
2
def findAllRecipes(
3
self, recipes: List[str], ingredients: List[List[str]], supplies: List[str]
4
) -> List[str]:
5

6
graph = defaultdict(list)
7
in_degree = defaultdict(int)
8
for r, ing in zip(recipes, ingredients):
9
for i in ing:
10
graph[i].append(r)
11
in_degree[r] += 1
12

13
queue = supplies[::]
14
res = []
15
while queue:
16
ing = queue.pop(0)
17
if ing in recipes:
18
res.append(ing)
19

20
for child in graph[ing]:
21
in_degree[child] -= 1
22
if in_degree[child] == 0:
23
queue.append(child)
24

25
return res

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0