LeetCode 2115: Find All Possible Recipes from Given Supplies Solution

Master LeetCode problem 2115 (Find All Possible Recipes from Given Supplies), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2115. Find All Possible Recipes from Given Supplies

Problem Explanation

Explanation:

To solve this problem, we can use a recursive approach to find all possible recipes that can be created from the initial supplies. We will start by checking each recipe in the recipes array and see if we have all the required ingredients for that recipe. If we have all the ingredients for a recipe, we can add that recipe to our list of created recipes and recursively check if any other recipes can be created using this newly created recipe as an ingredient.

Algorithm:

  1. Create a Set to store the created recipes.
  2. Create a helper method createRecipe that takes the recipe name as a parameter and checks if all the required ingredients are available.
  3. In the createRecipe method:
    • Check if the recipe is already created, if yes, return early.
    • Check if all ingredients for the given recipe are available in the supplies.
    • If all ingredients are available, add the recipe to the created recipes set.
    • Recursively call createRecipe for each ingredient of the given recipe.
  4. Iterate through each recipe in the recipes array and call createRecipe for each recipe.
  5. Return the list of created recipes.

Time Complexity:

The time complexity of this algorithm is O(n * m), where n is the number of recipes and m is the maximum number of ingredients in a single recipe.

Space Complexity:

The space complexity of this algorithm is O(n * m) for the recursive calls and storing created recipes.

:

Solution Code

import java.util.*;

class Solution {
    public List<String> findRecipes(String[] recipes, String[][] ingredients, String[] supplies) {
        Set<String> createdRecipes = new HashSet<>();

        // Helper method to recursively create recipes
        // with all available ingredients
        createRecipe(createdRecipes, recipes, ingredients, supplies);

        return new ArrayList<>(createdRecipes);
    }

    private void createRecipe(Set<String> createdRecipes, String[] recipes, String[][] ingredients, String[] supplies) {
        for (int i = 0; i < recipes.length; i++) {
            if (createdRecipes.contains(recipes[i])) {
                continue;
            }
            boolean canCreate = true;
            for (String ingredient : ingredients[i]) {
                if (!createdRecipes.contains(ingredient) && !Arrays.asList(supplies).contains(ingredient)) {
                    canCreate = false;
                    break;
                }
            }
            if (canCreate) {
                createdRecipes.add(recipes[i]);
                for (String ingredient : ingredients[i]) {
                    createRecipe(createdRecipes, recipes, ingredients, supplies);
                }
            }
        }
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2115 (Find All Possible Recipes from Given Supplies)?

This page provides optimized solutions for LeetCode problem 2115 (Find All Possible Recipes from Given Supplies) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2115 (Find All Possible Recipes from Given Supplies)?

The time complexity for LeetCode 2115 (Find All Possible Recipes from Given Supplies) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2115 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2115 in Java, C++, or Python.

Back to LeetCode Solutions