Created
March 21, 2025 19:02
-
-
Save tatsuyax25/ff897f4d1ed0f38efdac4ccfdc2111c4 to your computer and use it in GitHub Desktop.
You have information about n different recipes. You are given a string array recipes and a 2D string array ingredients. The ith recipe has the name recipes[i], and you can create it if you have all the needed ingredients from ingredients[i]. A recipe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {string[]} recipes | |
* @param {string[][]} ingredients | |
* @param {string[]} supplies | |
* @return {string[]} | |
*/ | |
var findAllRecipes = function(recipes, ingredients, supplies) { | |
// Step 1: Initialization | |
const n = recipes.length; // Get the number of recipes | |
const graph = new Map(); // Represents dependencies: ingredients point to recipes they can help create | |
const indegree = new Map(); // Tracks the number of unmet ingredients needed for each recipe | |
const result = []; // Stores all recipes that can be successfully created | |
// Step 2: Build the dependency graph and indegree map | |
for (let i = 0; i < n; i++) { | |
const recipe = recipes[i]; // Current recipe name | |
const requiredIngredients = ingredients[i]; // Ingredients required for this recipe | |
indegree.set(recipe, requiredIngredients.length); // Set initial indegree based on the number of required ingredients | |
// Connect ingredients to the recipe they contribute to in the graph | |
for (const ing of requiredIngredients) { | |
if (!graph.has(ing)) { | |
graph.set(ing, []); // Initialize the ingredient entry with an empty array | |
} | |
graph.get(ing).push(recipe); // Add this recipe as dependent on the current ingredient | |
} | |
} | |
// Step 3: Initialize the queue with starting supplies | |
const queue = [...supplies]; // Supplies represent the ingredients that are readily available | |
// Step 4: Process the graph using Breadth-First Search (BFS) | |
while (queue.length > 0) { | |
const item = queue.shift(); // Dequeue the first item (an available ingredient or recipe) | |
if (!graph.has(item)) continue; // Skip if the item has no dependencies in the graph | |
for (const recipe of graph.get(item)) { | |
// Reduce the indegree for the dependent recipe, as one required ingredient is now available | |
indegree.set(recipe, indegree.get(recipe) - 1); | |
// If all required ingredients for the recipe are available, mark it as creatable | |
if (indegree.get(recipe) === 0) { | |
result.push(recipe); // Add recipe to the result list | |
queue.push(recipe); // Treat the newly created recipe as a supply for other recipes | |
} | |
} | |
} | |
// Step 5: Return the final list of creatable recipes | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment