Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/emahtab/find-all-possible-recipes-from-given-supplies
https://github.com/emahtab/find-all-possible-recipes-from-given-supplies
leetcode topological-sort
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/emahtab/find-all-possible-recipes-from-given-supplies
- Owner: eMahtab
- Created: 2022-02-28T15:10:23.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-10-27T11:28:38.000Z (3 months ago)
- Last Synced: 2024-10-27T13:05:49.371Z (3 months ago)
- Topics: leetcode, topological-sort
- Homepage:
- Size: 3.91 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Find all possible recipes from given supplies
## https://leetcode.com/problems/find-all-possible-recipes-from-given-supplies## Implementation 1 : Topological Sort
```java
class Solution {
public List findAllRecipes(String[] recipes, List> ingredients, String[] supplies) {
Map> map = new HashMap<>();
for(int i = 0; i < recipes.length; i++) {
String recipe = recipes[i];
map.putIfAbsent(recipe, new HashSet());
List items = ingredients.get(i);
for(String item : items) {
map.get(recipe).add(item);
}
}
List result = new ArrayList();
Queue queue = new LinkedList<>();
for(String supply : supplies)
queue.add(supply);
while(!queue.isEmpty()) {
String supply = queue.remove();
for(String recipe : map.keySet()) {
Set items = map.get(recipe);
if(items.contains(supply)) {
items.remove(supply);
if(items.size() == 0) {
result.add(recipe);
queue.add(recipe);
}
}
}
}
return result;
}
}
```