-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_2115.java
63 lines (59 loc) · 2.44 KB
/
_2115.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.fishercoder.solutions.thirdthousand;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
public class _2115 {
public static class Solution1 {
/*
* My completely original solution, topological sort template comes in pretty handy.
*/
public List<String> findAllRecipes(
String[] recipes, List<List<String>> ingredients, String[] supplies) {
Set<String> allRecipes = new HashSet<>();
Collections.addAll(allRecipes, recipes);
Set<String> allSupplies = new HashSet<>();
Collections.addAll(allSupplies, supplies);
Map<String, Integer> indegree = new HashMap<>();
Map<String, List<String>> adjList = new HashMap<>();
Map<String, List<String>> ingredientMap = new HashMap<>();
for (int i = 0; i < ingredients.size(); i++) {
int dependencyCount = 0;
for (String ingredient : ingredients.get(i)) {
if (allRecipes.contains(ingredient)) {
dependencyCount++;
List<String> list = adjList.getOrDefault(ingredient, new ArrayList<>());
list.add(recipes[i]);
adjList.put(ingredient, list);
}
}
indegree.put(recipes[i], dependencyCount);
ingredientMap.put(recipes[i], ingredients.get(i));
}
Queue<String> q = new LinkedList<>();
for (Map.Entry<String, Integer> entry : indegree.entrySet()) {
if (entry.getValue() == 0
&& allSupplies.containsAll(ingredientMap.get(entry.getKey()))) {
q.offer(entry.getKey());
}
}
List<String> result = new ArrayList<>();
while (!q.isEmpty()) {
String curr = q.poll();
result.add(curr);
for (String neighbor : adjList.getOrDefault(curr, new ArrayList<>())) {
indegree.put(neighbor, indegree.get(neighbor) - 1);
if (indegree.get(neighbor) == 0) {
q.offer(neighbor);
}
}
}
return result;
}
}
}