Skip to content

Commit 243ac89

Browse files
committed
Mar 21
1 parent 0e9610e commit 243ac89

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from collections import defaultdict, deque
2+
3+
4+
class Solution:
5+
def findAllRecipes(
6+
self, recipes: list[str], ingredients: list[list[str]], supplies: list[str]
7+
) -> list[str]:
8+
supplies = set(supplies)
9+
graph = defaultdict(set) # ingredient -> recipes
10+
in_degree = defaultdict(int) # recipe -> in-degree
11+
queue = deque()
12+
13+
for recipe, ingredient in zip(recipes, ingredients):
14+
not_available = 0
15+
for i in ingredient:
16+
if i not in supplies:
17+
not_available += 1
18+
graph[i].add(recipe)
19+
if not_available == 0:
20+
queue.append(recipe)
21+
else:
22+
in_degree[recipe] = not_available
23+
24+
result = []
25+
while queue:
26+
recipe = queue.popleft()
27+
result.append(recipe)
28+
for neighbour in graph[recipe]:
29+
in_degree[neighbour] -= 1
30+
if in_degree[neighbour] == 0:
31+
queue.append(neighbour)
32+
return result
33+
34+
35+
def main():
36+
recipes = ['bread']
37+
ingredients = [['yeast', 'flour']]
38+
supplies = ['yeast', 'flour', 'corn']
39+
assert Solution().findAllRecipes(recipes, ingredients, supplies) == ['bread']
40+
41+
recipes = ['bread', 'sandwich']
42+
ingredients = [['yeast', 'flour'], ['bread', 'meat']]
43+
supplies = ['yeast', 'flour', 'meat']
44+
assert Solution().findAllRecipes(recipes, ingredients, supplies) == ['bread', 'sandwich']
45+
46+
recipes = ['bread', 'sandwich', 'burger']
47+
ingredients = [['yeast', 'flour'], ['bread', 'meat'], ['sandwich', 'meat', 'bread']]
48+
supplies = ['yeast', 'flour', 'meat']
49+
assert Solution().findAllRecipes(recipes, ingredients, supplies) == ['bread', 'sandwich', 'burger']
50+
51+
52+
if __name__ == '__main__':
53+
main()

2025-03-March-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
| March 18 | [2401. Longest Nice Subarray](https://leetcode.com/problems/longest-nice-subarray/) | Medium | Unsolved |
2525
| March 19 | [3191. Minimum Operations to Make Binary Array Elements Equal to One I](https://leetcode.com/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-i/) | Medium | Solved |
2626
| March 20 | [3108. Minimum Cost Walk in Weighted Graph](https://leetcode.com/problems/minimum-cost-walk-in-weighted-graph/) | Hard | Unsolved |
27-
| March 21 | []() | | |
27+
| March 21 | [2115. Find All Possible Recipes from Given Supplies](https://leetcode.com/problems/find-all-possible-recipes-from-given-supplies/) | Medium | Unsolved |
2828
| March 22 | []() | | |
2929
| March 23 | []() | | |
3030
| March 24 | []() | | |
@@ -41,5 +41,5 @@
4141
| Level | Problems | Solved | Unsolved |
4242
| --- | --- | --- | --- |
4343
| Easy | 6 | 6 | 0 |
44-
| Medium | 13 | 6 | 7 |
44+
| Medium | 14 | 6 | 8 |
4545
| Hard | 1 | 0 | 1 |

0 commit comments

Comments
 (0)