diff --git a/2115. Find All Possible Recipes from Given Supplies b/2115. Find All Possible Recipes from Given Supplies new file mode 100644 index 0000000..78e2a04 --- /dev/null +++ b/2115. Find All Possible Recipes from Given Supplies @@ -0,0 +1,31 @@ +class Solution { +public: +vector findAllRecipes(vector& recipes, vector>& ingredients, vector& supplies) { + unordered_set is_available(supplies.begin(), supplies.end()); + vector result; // final results + bool flag1 = true; // this current recipe added or not + + while(flag1){ + flag1 = false; + + for(int i = 0; i < recipes.size(); ++i){ + if(is_available.count(recipes[i]))continue; + + bool flag2 = true; + for(const string& ing : ingredients[i]){ + if (!is_available.count(ing)) { + flag2 = false; break; + } + } + + if(flag2){ + is_available.insert(recipes[i]); // further can used as supply + result.push_back(recipes[i]); + flag1 = true; + } + } + } + + return result; +} +};