2115. Find All Possible Recipes from Given Supplies

T: O(V+E)
S: O(n*m + m), ingredients: n, recipes: m
class Solution {
    public List<String> findAllRecipes(String[] recipes, List<List<String>> ingredients, String[] supplies) {
        Map<String, List<String>> graph = new HashMap<>();
        Map<String, Integer> indegree = new HashMap<>();
        Set<String> recipesSet = new HashSet<>();
        
        for (int i = 0; i < recipes.length; i++) {
            recipesSet.add(recipes[i]);
            for (int j = 0; j < ingredients.get(i).size(); j++) {
                String from = ingredients.get(i).get(j);
                String to = recipes[i];
                
                graph.putIfAbsent(from, new ArrayList<>());
                graph.get(from).add(to);
                
                indegree.put(to, indegree.getOrDefault(to, 0)+1);
            }
        }
        
        // this question's begin is using supplies
        Queue<String> queue = new LinkedList<>();
        for (String supply : supplies) {
            queue.offer(supply);
        }
        
        List<String> res = new ArrayList<>();
        while (!queue.isEmpty()) {
            String from = queue.poll();
            if (recipesSet.contains(from)) {
                res.add(from);
            }
            for (String to : graph.getOrDefault(from, new ArrayList<>())) {
                indegree.put(to, indegree.get(to) - 1);
                if (indegree.get(to) == 0) {
                    queue.offer(to);
                }
            }
        }
        return res;
    }
}

/*

recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread","meat"]]
supplies = ["yeast","flour","corn"]


"yeast", \bread
"flour" /     \  sandwich
          meat/      
          
supplies = ["yeast","flour","meat"] => indegree = 0


"yeast"   bread   sandwich   burger
"flour"   meat    meat
                  bread
                  
use topology sort:

ingredients: from
recipes: to
supplies: are the begins

T: O(V+E)
S: O(n*m + m), ingredients: n, recipes: m
*/

better one:

Last updated

Was this helpful?