@@ -63,13 +63,130 @@ We can create "burger" since we have the ingredient "meat" a
63
63
### ** Python3**
64
64
65
65
``` python
66
-
66
+ class Solution :
67
+ def findAllRecipes (self , recipes : List[str ], ingredients : List[List[str ]], supplies : List[str ]) -> List[str ]:
68
+ g = defaultdict(list )
69
+ indeg = defaultdict(int )
70
+ for a, b in zip (recipes, ingredients):
71
+ for v in b:
72
+ g[v].append(a)
73
+ indeg[a] += len (b)
74
+ q = deque(supplies)
75
+ ans = []
76
+ while q:
77
+ for _ in range (len (q)):
78
+ i = q.popleft()
79
+ for j in g[i]:
80
+ indeg[j] -= 1
81
+ if indeg[j] == 0 :
82
+ ans.append(j)
83
+ q.append(j)
84
+ return ans
67
85
```
68
86
69
87
### ** Java**
70
88
71
89
``` java
90
+ class Solution {
91
+ public List<String > findAllRecipes (String [] recipes , List<List<String > > ingredients , String [] supplies ) {
92
+ Map<String , List<String > > g = new HashMap<> ();
93
+ Map<String , Integer > indeg = new HashMap<> ();
94
+ for (int i = 0 ; i < recipes. length; ++ i) {
95
+ for (String v : ingredients. get(i)) {
96
+ g. computeIfAbsent(v, k - > new ArrayList<> ()). add(recipes[i]);
97
+ }
98
+ indeg. put(recipes[i], ingredients. get(i). size());
99
+ }
100
+ Deque<String > q = new ArrayDeque<> ();
101
+ for (String s : supplies) {
102
+ q. offer(s);
103
+ }
104
+ List<String > ans = new ArrayList<> ();
105
+ while (! q. isEmpty()) {
106
+ for (int n = q. size(); n > 0 ; -- n) {
107
+ String i = q. pollFirst();
108
+ for (String j : g. getOrDefault(i, Collections . emptyList())) {
109
+ indeg. put(j, indeg. get(j) - 1 );
110
+ if (indeg. get(j) == 0 ) {
111
+ ans. add(j);
112
+ q. offer(j);
113
+ }
114
+ }
115
+ }
116
+ }
117
+ return ans;
118
+ }
119
+ }
120
+ ```
121
+
122
+ ### ** C++**
123
+
124
+ ``` cpp
125
+ class Solution {
126
+ public:
127
+ vector<string > findAllRecipes(vector<string >& recipes, vector<vector<string >>& ingredients, vector<string >& supplies) {
128
+ unordered_map<string, vector<string >> g;
129
+ unordered_map<string, int> indeg;
130
+ for (int i = 0; i < recipes.size(); ++i) {
131
+ for (auto& v : ingredients[ i] ) {
132
+ g[ v] .push_back(recipes[ i] );
133
+ }
134
+ indeg[ recipes[ i]] = ingredients[ i] .size();
135
+ }
136
+ queue<string > q;
137
+ for (auto& s : supplies) {
138
+ q.push(s);
139
+ }
140
+ vector<string > ans;
141
+ while (!q.empty()) {
142
+ for (int n = q.size(); n; --n) {
143
+ auto i = q.front();
144
+ q.pop();
145
+ for (auto j : g[ i] ) {
146
+ if (--indeg[ j] == 0) {
147
+ ans.push_back(j);
148
+ q.push(j);
149
+ }
150
+ }
151
+ }
152
+ }
153
+ return ans;
154
+ }
155
+ };
156
+ ```
72
157
158
+ ### **Go**
159
+
160
+ ```go
161
+ func findAllRecipes(recipes []string, ingredients [][]string, supplies []string) []string {
162
+ g := map[string][]string{}
163
+ indeg := map[string]int{}
164
+ for i, a := range recipes {
165
+ for _, b := range ingredients[i] {
166
+ g[b] = append(g[b], a)
167
+ }
168
+ indeg[a] = len(ingredients[i])
169
+ }
170
+ q := []string{}
171
+ for _, s := range supplies {
172
+ q = append(q, s)
173
+ }
174
+ ans := []string{}
175
+ for len(q) > 0 {
176
+ for n := len(q); n > 0; n-- {
177
+ i := q[0]
178
+ q = q[1:]
179
+ for _, j := range g[i] {
180
+ indeg[j]--
181
+ if indeg[j] == 0 {
182
+ ans = append(ans, j)
183
+ q = append(q, j)
184
+ }
185
+ }
186
+ }
187
+ }
188
+ return ans
189
+ }
73
190
```
74
191
75
192
### ** TypeScript**
0 commit comments