-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path2115.cpp
35 lines (34 loc) · 1.26 KB
/
2115.cpp
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
class Solution {
public:
vector<string> findAllRecipes(vector<string>& recipes, vector<vector<string>>& ingredients, vector<string>& supplies) {
unordered_map<string, int> inDegree;
unordered_map<string, vector<string>> graph; //ingred->recipe
unordered_set<string> suppliesHash;
for (auto& supply : supplies) suppliesHash.insert(supply);
int n = recipes.size();
for (int i = 0; i < n; ++i) {
inDegree[recipes[i]] = 0;
for (int j = 0; j < ingredients[i].size(); j++) {
if (suppliesHash.find(ingredients[i][j]) == suppliesHash.end()) {
inDegree[recipes[i]]++;
graph[ingredients[i][j]].push_back(recipes[i]);
}
}
}
vector<string> res;
queue<string> q;
for (auto& element : inDegree) {
if (element.second == 0) q.push(element.first);
}
while (!q.empty()) {
string recipe = q.front();
q.pop();
res.push_back(recipe);
for (auto& nextRecipe : graph[recipe]) {
inDegree[nextRecipe]--;
if (inDegree[nextRecipe] == 0) q.push(nextRecipe);
}
}
return res;
}
};