aboutsummaryrefslogtreecommitdiff
path: root/possible_recipes_supplies.py
blob: 63fe5ff83e8b755148e388414be98cdc652ff46f (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 2115. Find All Possible Recipes from Given Supplies
from collections import defaultdict, Counter

"""
you have information about 'n' different recipes. you are given a string
array 'recipes' and a 2d string array 'ingredients'. the i'th recipe has the
name 'recipes[i]', and you can create it is you have all the needed
ingredients from 'ingredients[i]'. ingredients to a recipe may need to be
created from other recipes, ie. 'ingredients[i]' may contain a string that is
in 'recipes'. 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. return a list of all the recipes that you can create. you may return
the answer in any order.
"""


class Solution(object):
    def findAllRecipes(self, recipes, ingredients, supplies):
        """
        :type recipes: List[str]
        :type ingredients: List[List[str]]
        :type supplies: List[str]
        :rtype: List[str]
        """
        to_recipe, in_deg = defaultdict(set), Counter()
        for i, j in zip(recipes, ingredients):
            for k in j:
                to_recipe[k].add(i)
            in_deg[i] = len(j)
        ans = []
        for i in supplies:
            for j in to_recipe.pop(i, set()):
                in_deg[j] -= 1
                if in_deg[j] == 0:
                    supplies.append(j)
                    ans.append(j)
        return ans


if __name__ == "__main__":
    obj = Solution()
    print(
        obj.findAllRecipes(
            recipes=["bread"],
            ingredients=[["yeast", "flour"]],
            supplies=["yeast", "flour", "corn"],
        )
    )
    print(
        obj.findAllRecipes(
            recipes=["bread", "sandwich"],
            ingredients=[["yeast", "flour"], ["bread", "meat"]],
            supplies=["yeast", "flour", "meat"],
        )
    )
    print(
        obj.findAllRecipes(
            recipes=["bread", "sandwich", "burger"],
            ingredients=[
                ["yeast", "flour"],
                ["bread", "meat"],
                ["sandwich", "meat", "bread"],
            ],
            supplies=["yeast", "flour", "meat"],
        )
    )