Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public List<String> findAllRecipes(String[] recipes, List<List<String>> ingredients, String[] supplies) {
- List<String> ans = new ArrayList<>();
- Set<String> available = new HashSet<>(Arrays.asList(supplies));
- Map<String, Set<String>> ingredientToRecipes = new HashMap<>();
- Map<String, Integer> inDegree = new HashMap<>();
- for (int i = 0; i < recipes.length; ++i) {
- int nonAvailable = 0;
- for (String ing : ingredients.get(i)) {
- if (!available.contains(ing)) {
- ingredientToRecipes.computeIfAbsent(ing, s -> new HashSet<>()).add(recipes[i]);
- ++nonAvailable;
- }
- }
- if (nonAvailable == 0) {
- ans.add(recipes[i]);
- }else {
- inDegree.put(recipes[i], nonAvailable);
- }
- }
- for (int i = 0; i < ans.size(); ++i) {
- String recipe = ans.get(i);
- if (ingredientToRecipes.containsKey(recipe)) {
- for (String rcp : ingredientToRecipes.remove(recipe)) {
- if (inDegree.merge(rcp, -1, Integer::sum) == 0) {
- ans.add(rcp);
- }
- }
- }
- }
- return ans;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement