Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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;
}
}
```