일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 43. Multiply Strings
- Python Implementation
- 109. Convert Sorted List to Binary Search Tree
- 컴퓨터의 구조
- Decorator
- 315. Count of Smaller Numbers After Self
- LeetCode
- 30. Substring with Concatenation of All Words
- Class
- attribute
- 운영체제
- Python
- 밴픽
- 715. Range Module
- 파이썬
- shiba
- t1
- Protocol
- 프로그래머스
- Python Code
- data science
- Regular Expression
- Substring with Concatenation of All Words
- kaggle
- Convert Sorted List to Binary Search Tree
- concurrency
- DWG
- 시바견
- Generator
- iterator
Archives
- Today
- Total
Scribbling
LeetCode: 2115. Find All Possible Recipes from Given Supplies 본문
Computer Science/Coding Test
LeetCode: 2115. Find All Possible Recipes from Given Supplies
focalpoint 2022. 5. 3. 09:43For a given recipe, we need to check whether all the necessary elements can be supplied.
If an element belongs to supplies, it's a piece of cake.
Now the question is to deal with elements that belong to recipes.
We can think of recipes as a graph with cycles.
Plus, if there's a cycle among certain recipes, they can never be cooked.
class Solution:
def findAllRecipes(self, recipes: List[str], ingredients: List[List[str]], supplies: List[str]) -> List[str]:
ret = set()
self.recipes, self.supplies = set(recipes), set(supplies)
self.ingredients = {recipes[i]: ingredients[i] for i in range(len(recipes))}
self.dp = {}
for recipe in recipes:
if self.canCook(recipe):
ret.add(recipe)
return ret
def canCook(self, recipe):
if recipe in self.dp:
if self.dp[recipe] == 'cooked':
return True
# 'cannot be cooked' or 'cooking'
else:
return False
# never cooked before
self.dp[recipe] = 'cooking'
for ingredient in self.ingredients[recipe]:
if ingredient in self.supplies:
continue
if ingredient in self.recipes:
if self.canCook(ingredient):
continue
else:
self.dp[recipe] = 'cannot be cooked'
return False
else:
self.dp[recipe] = 'cannot be cooked'
return False
self.dp[recipe] = 'cooked'
return True
'Computer Science > Coding Test' 카테고리의 다른 글
Overlapping Intervals/Ranges (0) | 2023.04.24 |
---|---|
[Dynamic Programming] Practice Question (0) | 2023.04.14 |
LeetCode: 1032. Stream of Characters (0) | 2022.03.25 |
LeetCode: 2034. Stock Price Fluctuation (0) | 2022.03.23 |
LeetCode: 715. Range Module (0) | 2022.03.18 |