Skip to content

Commit 1da375b

Browse files
committed
Sync LeetCode submission Runtime - 699 ms (8.19%), Memory - 19.7 MB (79.98%)
1 parent bb6b443 commit 1da375b

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<p>You have information about <code>n</code> different recipes. You are given a string array <code>recipes</code> and a 2D string array <code>ingredients</code>. The <code>i<sup>th</sup></code> recipe has the name <code>recipes[i]</code>, and you can <strong>create</strong> it if you have <strong>all</strong> the needed ingredients from <code>ingredients[i]</code>. A recipe can also be an ingredient for <strong>other </strong>recipes, i.e., <code>ingredients[i]</code> may contain a string that is in <code>recipes</code>.</p>
2+
3+
<p>You are also given a string array <code>supplies</code> containing all the ingredients that you initially have, and you have an infinite supply of all of them.</p>
4+
5+
<p>Return <em>a list of all the recipes that you can create. </em>You may return the answer in <strong>any order</strong>.</p>
6+
7+
<p>Note that two recipes may contain each other in their ingredients.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre>
13+
<strong>Input:</strong> recipes = [&quot;bread&quot;], ingredients = [[&quot;yeast&quot;,&quot;flour&quot;]], supplies = [&quot;yeast&quot;,&quot;flour&quot;,&quot;corn&quot;]
14+
<strong>Output:</strong> [&quot;bread&quot;]
15+
<strong>Explanation:</strong>
16+
We can create &quot;bread&quot; since we have the ingredients &quot;yeast&quot; and &quot;flour&quot;.
17+
</pre>
18+
19+
<p><strong class="example">Example 2:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> recipes = [&quot;bread&quot;,&quot;sandwich&quot;], ingredients = [[&quot;yeast&quot;,&quot;flour&quot;],[&quot;bread&quot;,&quot;meat&quot;]], supplies = [&quot;yeast&quot;,&quot;flour&quot;,&quot;meat&quot;]
23+
<strong>Output:</strong> [&quot;bread&quot;,&quot;sandwich&quot;]
24+
<strong>Explanation:</strong>
25+
We can create &quot;bread&quot; since we have the ingredients &quot;yeast&quot; and &quot;flour&quot;.
26+
We can create &quot;sandwich&quot; since we have the ingredient &quot;meat&quot; and can create the ingredient &quot;bread&quot;.
27+
</pre>
28+
29+
<p><strong class="example">Example 3:</strong></p>
30+
31+
<pre>
32+
<strong>Input:</strong> recipes = [&quot;bread&quot;,&quot;sandwich&quot;,&quot;burger&quot;], ingredients = [[&quot;yeast&quot;,&quot;flour&quot;],[&quot;bread&quot;,&quot;meat&quot;],[&quot;sandwich&quot;,&quot;meat&quot;,&quot;bread&quot;]], supplies = [&quot;yeast&quot;,&quot;flour&quot;,&quot;meat&quot;]
33+
<strong>Output:</strong> [&quot;bread&quot;,&quot;sandwich&quot;,&quot;burger&quot;]
34+
<strong>Explanation:</strong>
35+
We can create &quot;bread&quot; since we have the ingredients &quot;yeast&quot; and &quot;flour&quot;.
36+
We can create &quot;sandwich&quot; since we have the ingredient &quot;meat&quot; and can create the ingredient &quot;bread&quot;.
37+
We can create &quot;burger&quot; since we have the ingredient &quot;meat&quot; and can create the ingredients &quot;bread&quot; and &quot;sandwich&quot;.
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>n == recipes.length == ingredients.length</code></li>
45+
<li><code>1 &lt;= n &lt;= 100</code></li>
46+
<li><code>1 &lt;= ingredients[i].length, supplies.length &lt;= 100</code></li>
47+
<li><code>1 &lt;= recipes[i].length, ingredients[i][j].length, supplies[k].length &lt;= 10</code></li>
48+
<li><code>recipes[i], ingredients[i][j]</code>, and <code>supplies[k]</code> consist only of lowercase English letters.</li>
49+
<li>All the values of <code>recipes</code> and <code>supplies</code>&nbsp;combined are unique.</li>
50+
<li>Each <code>ingredients[i]</code> does not contain any duplicate values.</li>
51+
</ul>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Approach 1: Breadth-First Search (BFS)
2+
3+
# n = len(reciples), m = total ingredients, s = len(supplies)
4+
# Time: O(n * m + s)
5+
# Space: O(n + s)
6+
7+
from collections import deque
8+
9+
class Solution:
10+
def findAllRecipes(self, recipes: List[str], ingredients: List[List[str]], supplies: List[str]) -> List[str]:
11+
available = set(supplies)
12+
13+
# Recipe indices
14+
recipe_queue = deque(range(len(recipes)))
15+
created_recipes = []
16+
last_size = -1
17+
18+
while len(available) > last_size:
19+
last_size = len(available)
20+
queue_size = len(recipe_queue)
21+
22+
while queue_size > 0:
23+
queue_size -= 1
24+
recipe_idx = recipe_queue.popleft()
25+
26+
if all(ingredient in available for ingredient in ingredients[recipe_idx]):
27+
available.add(recipes[recipe_idx])
28+
created_recipes.append(recipes[recipe_idx])
29+
else:
30+
recipe_queue.append(recipe_idx)
31+
32+
return created_recipes
33+

0 commit comments

Comments
 (0)