Skip to content

Commit c9ad8c7

Browse files
authored
Merge pull request soapyigu#345 from soapyigu/Graph
Add a solution to Find All Possible Recipes from Given Supplies
2 parents b3eff02 + 506fbb2 commit c9ad8c7

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/find-all-possible-recipes-from-given-supplies/
3+
* Primary idea: Topological sort, keep each recipe with ingredients for a inDegree number and
4+
* to recipe list, use a queue to decrease inDegree and form the result
5+
*
6+
* Time Complexity: O(n ^ 2), Space Complexity: O(n + m),
7+
* n represents the length of recipes, m represents the length of supplies
8+
*/
9+
10+
class FindAllPossibleRecipesGivenSupplies {
11+
func findAllRecipes(_ recipes: [String], _ ingredients: [[String]], _ supplies: [String]) -> [String] {
12+
var (inDegrees, toRecipes) = buildGraph(recipes, ingredients)
13+
var res = [String](), queue = supplies
14+
15+
while !queue.isEmpty {
16+
let food = queue.removeFirst()
17+
18+
for recipe in toRecipes[food] ?? [] {
19+
inDegrees[recipe]! -= 1
20+
21+
if inDegrees[recipe] == 0 {
22+
res.append(recipe)
23+
queue.append(recipe)
24+
}
25+
}
26+
}
27+
28+
return res
29+
}
30+
31+
private func buildGraph(_ recipes: [String], _ ingredients: [[String]]) -> ([String: Int], [String: [String]]) {
32+
var inDegrees = [String: Int]()
33+
var toRecipes = [String: [String]]()
34+
35+
for i in 0..<recipes.count {
36+
let (recipe, recipeIngredients) = (recipes[i], ingredients[i])
37+
38+
for ingredient in recipeIngredients {
39+
inDegrees[recipe, default: 0] += 1
40+
toRecipes[ingredient, default: []].append(recipe)
41+
}
42+
}
43+
44+
return (inDegrees, toRecipes)
45+
}
46+
}

0 commit comments

Comments
 (0)