{"id":9264,"date":"2021-12-26T07:47:05","date_gmt":"2021-12-26T15:47:05","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9264"},"modified":"2021-12-26T07:47:28","modified_gmt":"2021-12-26T15:47:28","slug":"leetcode-2115-find-all-possible-recipes-from-given-supplies","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2115-find-all-possible-recipes-from-given-supplies\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2115. Find All Possible Recipes from Given Supplies"},"content":{"rendered":"\n<p>You have information about&nbsp;<code>n<\/code>&nbsp;different recipes. You are given a string array&nbsp;<code>recipes<\/code>&nbsp;and a 2D string array&nbsp;<code>ingredients<\/code>. The&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;recipe has the name&nbsp;<code>recipes[i]<\/code>, and you can&nbsp;<strong>create<\/strong>&nbsp;it if you have&nbsp;<strong>all<\/strong>&nbsp;the needed ingredients from&nbsp;<code>ingredients[i]<\/code>. Ingredients to a recipe may need to be created from&nbsp;<strong>other&nbsp;<\/strong>recipes, i.e.,&nbsp;<code>ingredients[i]<\/code>&nbsp;may contain a string that is in&nbsp;<code>recipes<\/code>.<\/p>\n\n\n\n<p>You are also given a string array&nbsp;<code>supplies<\/code>&nbsp;containing all the ingredients that you initially have, and you have an infinite supply of all of them.<\/p>\n\n\n\n<p>Return&nbsp;<em>a list of all the recipes that you can create.&nbsp;<\/em>You may return the answer in&nbsp;<strong>any order<\/strong>.<\/p>\n\n\n\n<p>Note that two recipes may contain each other in their ingredients.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> recipes = [\"bread\"], ingredients = [[\"yeast\",\"flour\"]], supplies = [\"yeast\",\"flour\",\"corn\"]\n<strong>Output:<\/strong> [\"bread\"]\n<strong>Explanation:<\/strong>\nWe can create \"bread\" since we have the ingredients \"yeast\" and \"flour\".\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> recipes = [\"bread\",\"sandwich\"], ingredients = [[\"yeast\",\"flour\"],[\"bread\",\"meat\"]], supplies = [\"yeast\",\"flour\",\"meat\"]\n<strong>Output:<\/strong> [\"bread\",\"sandwich\"]\n<strong>Explanation:<\/strong>\nWe can create \"bread\" since we have the ingredients \"yeast\" and \"flour\".\nWe can create \"sandwich\" since we have the ingredient \"meat\" and can create the ingredient \"bread\".\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> recipes = [\"bread\",\"sandwich\",\"burger\"], ingredients = [[\"yeast\",\"flour\"],[\"bread\",\"meat\"],[\"sandwich\",\"meat\",\"bread\"]], supplies = [\"yeast\",\"flour\",\"meat\"]\n<strong>Output:<\/strong> [\"bread\",\"sandwich\",\"burger\"]\n<strong>Explanation:<\/strong>\nWe can create \"bread\" since we have the ingredients \"yeast\" and \"flour\".\nWe can create \"sandwich\" since we have the ingredient \"meat\" and can create the ingredient \"bread\".\nWe can create \"burger\" since we have the ingredient \"meat\" and can create the ingredients \"bread\" and \"sandwich\".\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == recipes.length == ingredients.length<\/code><\/li><li><code>1 &lt;= n &lt;= 100<\/code><\/li><li><code>1 &lt;= ingredients[i].length, supplies.length &lt;= 100<\/code><\/li><li><code>1 &lt;= recipes[i].length, ingredients[i][j].length, supplies[k].length &lt;= 10<\/code><\/li><li><code>recipes[i], ingredients[i][j]<\/code>, and&nbsp;<code>supplies[k]<\/code>&nbsp;consist only of lowercase English letters.<\/li><li>All the values of&nbsp;<code>recipes<\/code>&nbsp;and&nbsp;<code>supplies<\/code>&nbsp;combined are unique.<\/li><li>Each&nbsp;<code>ingredients[i]<\/code>&nbsp;does not contain any duplicate values.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<string> findAllRecipes(vector<string>& recipes, vector<vector<string>>& ingredients, vector<string>& supplies) {\n    const int n = recipes.size();\n    unordered_set<string> s(begin(supplies), end(supplies));\n    vector<string> ans;\n    vector<int> seen(n);\n    while (true) {      \n      bool newSupply = false;  \n      for (int i = 0; i < n; ++i) {\n        if (seen[i]) continue;\n        bool hasAll = true;\n        for (const string&#038; ingredient : ingredients[i])\n          if (!s.count(ingredient)) {\n            hasAll = false; \n            break;\n          }\n        if (!hasAll) continue;\n        ans.push_back(recipes[i]);\n        seen[i] = 1;\n        s.insert(recipes[i]);\n        newSupply = true;              \n      }\n      if (!newSupply) break;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You have information about&nbsp;n&nbsp;different recipes. You are given a string array&nbsp;recipes&nbsp;and a 2D string array&nbsp;ingredients. The&nbsp;ith&nbsp;recipe has the name&nbsp;recipes[i], and you can&nbsp;create&nbsp;it if you have&nbsp;all&nbsp;the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[77,82,177],"class_list":["post-9264","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-graph","tag-hashtable","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9264"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9264\/revisions"}],"predecessor-version":[{"id":9266,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9264\/revisions\/9266"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}