-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindAllRecipes.py
More file actions
27 lines (24 loc) · 989 Bytes
/
findAllRecipes.py
File metadata and controls
27 lines (24 loc) · 989 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution:
def findAllRecipes(self, recipes: List[str], ingredients: List[List[str]], supplies: List[str]) -> List[str]:
supplies = set(supplies)
dependency = {}
indegrees = {}
for i,recipe in enumerate(recipes):
for ingredient in ingredients[i]:
if ingredient not in supplies:
indegrees[recipe] = indegrees.get(recipe,0) + 1
dependency[ingredient] = dependency.get(ingredient,[]) + [recipe]
queue = deque()
for recipe in recipes:
if recipe not in indegrees:
queue.append(recipe)
res = []
while queue:
recipe = queue.popleft()
res.append(recipe)
deps = dependency.get(recipe,[])
for dep in deps:
indegrees[dep] -= 1
if indegrees[dep] == 0:
queue.append(dep)
return res