2115. Find All Possible Recipes from Given Supplies LeetCode Solution
In this guide, you will get 2115. Find All Possible Recipes from Given Supplies LeetCode Solution with the best time and space complexity. The solution to Find All Possible Recipes from Given Supplies problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find All Possible Recipes from Given Supplies solution in C++
- Find All Possible Recipes from Given Supplies solution in Java
- Find All Possible Recipes from Given Supplies solution in Python
- Additional Resources
Problem Statement of Find All Possible Recipes from Given Supplies
You have information about n different recipes. You are given a string array recipes and a 2D string array ingredients. The ith recipe has the name recipes[i], and you can create it if you have all the needed ingredients from ingredients[i]. A recipe can also be an ingredient for other recipes, i.e., 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.
Note that two recipes may contain each other in their ingredients.
Example 1:
Input: recipes = [“bread”], ingredients = [[“yeast”,”flour”]], supplies = [“yeast”,”flour”,”corn”]
Output: [“bread”]
Explanation:
We can create “bread” since we have the ingredients “yeast” and “flour”.
Example 2:
Input: recipes = [“bread”,”sandwich”], ingredients = [[“yeast”,”flour”],[“bread”,”meat”]], supplies = [“yeast”,”flour”,”meat”]
Output: [“bread”,”sandwich”]
Explanation:
We can create “bread” since we have the ingredients “yeast” and “flour”.
We can create “sandwich” since we have the ingredient “meat” and can create the ingredient “bread”.
Example 3:
Input: recipes = [“bread”,”sandwich”,”burger”], ingredients = [[“yeast”,”flour”],[“bread”,”meat”],[“sandwich”,”meat”,”bread”]], supplies = [“yeast”,”flour”,”meat”]
Output: [“bread”,”sandwich”,”burger”]
Explanation:
We can create “bread” since we have the ingredients “yeast” and “flour”.
We can create “sandwich” since we have the ingredient “meat” and can create the ingredient “bread”.
We can create “burger” since we have the ingredient “meat” and can create the ingredients “bread” and “sandwich”.
Constraints:
n == recipes.length == ingredients.length
1 <= n <= 100
1 <= ingredients[i].length, supplies.length <= 100
1 <= recipes[i].length, ingredients[i][j].length, supplies[k].length <= 10
recipes[i], ingredients[i][j], and supplies[k] consist only of lowercase English letters.
All the values of recipes and supplies combined are unique.
Each ingredients[i] does not contain any duplicate values.
Complexity Analysis
- Time Complexity: O(|V| + |E|)
- Space Complexity: O(|V| + |E|)
2115. Find All Possible Recipes from Given Supplies LeetCode Solution in C++
class Solution {
public:
vector<string> findAllRecipes(vector<string>& recipes,
vector<vector<string>>& ingredients,
vector<string>& supplies) {
vector<string> ans;
unordered_set<string> suppliesSet(supplies.begin(), supplies.end());
unordered_map<string, vector<string>> graph;
unordered_map<string, int> inDegrees;
queue<string> q;
// Build the graph.
for (int i = 0; i < recipes.size(); ++i)
for (const string& ingredient : ingredients[i])
if (!suppliesSet.contains(ingredient)) {
graph[ingredient].push_back(recipes[i]);
++inDegrees[recipes[i]];
}
// Perform topological sorting.
for (const string& recipe : recipes)
if (!inDegrees.contains(recipe))
q.push(recipe);
while (!q.empty()) {
const string u = q.front();
q.pop();
ans.push_back(u);
if (!graph.contains(u))
continue;
for (const string& v : graph[u])
if (--inDegrees[v] == 0)
q.push(v);
}
return ans;
}
};
/* code provided by PROGIEZ */
2115. Find All Possible Recipes from Given Supplies LeetCode Solution in Java
class Solution {
public List<String> findAllRecipes(String[] recipes, List<List<String>> ingredients,
String[] supplies) {
List<String> ans = new ArrayList<>();
Set<String> suppliesSet = new HashSet<>();
for (final String supply : supplies)
suppliesSet.add(supply);
Map<String, List<String>> graph = new HashMap<>();
Map<String, Integer> inDegrees = new HashMap<>();
// Build the graph.
for (int i = 0; i < recipes.length; ++i)
for (final String ingredient : ingredients.get(i))
if (!suppliesSet.contains(ingredient)) {
graph.putIfAbsent(ingredient, new ArrayList<>());
graph.get(ingredient).add(recipes[i]);
inDegrees.merge(recipes[i], 1, Integer::sum);
}
// Perform topological sorting.
Queue<String> q = Arrays.stream(recipes)
.filter(recipe -> inDegrees.getOrDefault(recipe, 0) == 0)
.collect(Collectors.toCollection(ArrayDeque::new));
while (!q.isEmpty()) {
final String u = q.poll();
ans.add(u);
if (!graph.containsKey(u))
continue;
for (final String v : graph.get(u)) {
inDegrees.merge(v, -1, Integer::sum);
if (inDegrees.get(v) == 0)
q.offer(v);
}
}
return ans;
}
}
// code provided by PROGIEZ
2115. Find All Possible Recipes from Given Supplies LeetCode Solution in Python
class Solution:
def findAllRecipes(
self,
recipes: list[str],
ingredients: list[list[str]],
supplies: list[str],
) -> list[str]:
ans = []
supplies = set(supplies)
graph = collections.defaultdict(list)
inDegrees = collections.Counter()
q = collections.deque()
# Build the graph.
for i, recipe in enumerate(recipes):
for ingredient in ingredients[i]:
if ingredient not in supplies:
graph[ingredient].append(recipe)
inDegrees[recipe] += 1
# Perform topological sorting.
for recipe in recipes:
if inDegrees[recipe] == 0:
q.append(recipe)
while q:
u = q.popleft()
ans.append(u)
for v in graph[u]:
inDegrees[v] -= 1
if inDegrees[v] == 0:
q.append(v)
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.