-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
165 lines (148 loc) · 5.1 KB
/
main.go
File metadata and controls
165 lines (148 loc) · 5.1 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Source: https://leetcode.com/problems/find-all-possible-recipes-from-given-supplies
// Title: Find All Possible Recipes from Given Supplies
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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 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.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
import "fmt"
// DFS
func findAllRecipes(recipes []string, ingredients [][]string, supplies []string) []string {
r := len(recipes)
s := len(supplies)
// Prepare depend map
dependMap := make(map[string][]string, r+s) // map ingredient to recipe
for i, recipe := range recipes {
for _, ingredient := range ingredients[i] {
dependMap[ingredient] = append(dependMap[ingredient], recipe)
}
}
// Prepare recipe map
recipeMap := make(map[string]map[string]bool) // map recipe to ingredient
for i, recipe := range recipes {
recipeMap[recipe] = make(map[string]bool, len(ingredients[i]))
for _, ingredient := range ingredients[i] {
recipeMap[recipe][ingredient] = true
}
}
// DFS
visited := make(map[string]bool, r) // visited recipe (i.e. creatable)
var dfs func(item string)
dfs = func(item string) {
if visited[item] { // visited
return
}
if len(recipeMap[item]) > 0 { // not ready
return
}
visited[item] = true
for _, dependent := range dependMap[item] {
delete(recipeMap[dependent], item)
dfs(dependent)
}
}
for _, supply := range supplies {
dfs(supply)
}
res := make([]string, 0, r)
for _, recipe := range recipes {
if visited[recipe] {
res = append(res, recipe)
}
}
return res
}
// Topological Sort (Kahn's Algorithm)
func findAllRecipes2(recipes []string, ingredients [][]string, supplies []string) []string {
r := len(recipes)
s := len(supplies)
res := make([]string, 0, r)
// Prepare depend map
dependMap := make(map[string][]string, r+s) // map ingredient to recipe
for i, recipe := range recipes {
for _, ingredient := range ingredients[i] {
dependMap[ingredient] = append(dependMap[ingredient], recipe)
}
}
// Count in-degree
inDegree := make(map[string]int, r)
for i, recipe := range recipes {
inDegree[recipe] = len(ingredients[i])
}
// Loop through the queue
queue := supplies
for len(queue) > 0 {
item := queue[0]
queue = queue[1:]
for _, dependent := range dependMap[item] {
inDegree[dependent]--
if inDegree[dependent] == 0 {
res = append(res, dependent)
queue = append(queue, dependent)
}
}
}
return res
}
func main() {
fmt.Println(findAllRecipes2(
[]string{
"bread", "sandwich",
},
[][]string{
{"yeast", "flour"},
{"bread", "meat"},
},
[]string{
"yeast", "flour",
},
))
}