Loading problem...
You are building a crafting system for a game or manufacturing process. You have information about n different craftable items. You are given a string array items representing the names of all craftable items, and a 2D string array requirements where requirements[i] contains all the components needed to create items[i].
A craftable item can only be produced if all of its required components are available. Interestingly, a craftable item can also serve as a component for creating other items—meaning requirements[i] may include strings that appear in the items array itself.
You are also given a string array inventory containing all the base components you initially possess, with an unlimited supply of each.
Return a list of all items that you can successfully craft. The answer may be returned in any order.
Note: Two items may mutually require each other as components, which would make both impossible to craft unless one can be created independently first.
items = ["bread"]
requirements = [["yeast","flour"]]
inventory = ["yeast","flour","corn"]["bread"]We can craft "bread" because we have both required components "yeast" and "flour" in our inventory.
items = ["bread","sandwich"]
requirements = [["yeast","flour"],["bread","meat"]]
inventory = ["yeast","flour","meat"]["bread","sandwich"]We can craft "bread" since we have "yeast" and "flour". Once "bread" is craftable, we can then craft "sandwich" because we have "meat" and can produce "bread".
items = ["bread","sandwich","burger"]
requirements = [["yeast","flour"],["bread","meat"],["sandwich","meat","bread"]]
inventory = ["yeast","flour","meat"]["bread","sandwich","burger"]First, we craft "bread" using "yeast" and "flour". Then we craft "sandwich" using "bread" and "meat". Finally, we craft "burger" using "sandwich", "meat", and "bread"—all of which are now available.
Constraints