Skip to content

Commit 73af78e

Browse files
authored
Create 2115. Find All Possible Recipes from Given Supplies
1 parent 0c2e966 commit 73af78e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
vector<string> findAllRecipes(vector<string>& recipes, vector<vector<string>>& ingredients, vector<string>& supplies) {
4+
unordered_set<string> is_available(supplies.begin(), supplies.end());
5+
vector<string> result; // final results
6+
bool flag1 = true; // this current recipe added or not
7+
8+
while(flag1){
9+
flag1 = false;
10+
11+
for(int i = 0; i < recipes.size(); ++i){
12+
if(is_available.count(recipes[i]))continue;
13+
14+
bool flag2 = true;
15+
for(const string& ing : ingredients[i]){
16+
if (!is_available.count(ing)) {
17+
flag2 = false; break;
18+
}
19+
}
20+
21+
if(flag2){
22+
is_available.insert(recipes[i]); // further can used as supply
23+
result.push_back(recipes[i]);
24+
flag1 = true;
25+
}
26+
}
27+
}
28+
29+
return result;
30+
}
31+
};

0 commit comments

Comments
 (0)