2115. Find All Possible Recipes from Given Supplies #1461
-
Topics: You have information about You are also given a string array Return a list of all the recipes that you can create. You may return the answer in any order. Note that two recipes may contain each other in their ingredients. Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to determine all possible recipes that can be created given a set of initial supplies and recipes that may depend on each other. The key challenge is to handle dependencies between recipes and ensure that all required ingredients (either from initial supplies or other recipes) are available. Approach
Let's implement this solution in PHP: 2115. Find All Possible Recipes from Given Supplies <?php
/**
* @param String[] $recipes
* @param String[][] $ingredients
* @param String[] $supplies
* @return String[]
*/
function findAllRecipes($recipes, $ingredients, $supplies) {
$suppliesSet = array_flip($supplies);
$recipeNameMap = array();
foreach ($recipes as $idx => $r) {
$recipeNameMap[$r] = $idx;
}
$validRecipes = array();
$inDegree = array();
$adjacency = array();
foreach ($recipes as $i => $recipe) {
$currentIngredients = $ingredients[$i];
$isValid = true;
foreach ($currentIngredients as $ing) {
if (!isset($suppliesSet[$ing]) && !isset($recipeNameMap[$ing])) {
$isValid = false;
break;
}
}
if ($isValid) {
$dependencies = array();
foreach ($currentIngredients as $ing) {
if (isset($recipeNameMap[$ing]) && !isset($suppliesSet[$ing])) {
$dependencies[] = $ing;
}
}
$inDegree[$recipe] = count($dependencies);
foreach ($dependencies as $dep) {
if (!isset($adjacency[$dep])) {
$adjacency[$dep] = array();
}
$adjacency[$dep][] = $recipe;
}
$validRecipes[] = $recipe;
}
}
$queue = array();
foreach ($validRecipes as $r) {
if ($inDegree[$r] == 0) {
array_push($queue, $r);
}
}
$result = array();
while (!empty($queue)) {
$current = array_shift($queue);
array_push($result, $current);
if (isset($adjacency[$current])) {
foreach ($adjacency[$current] as $dependent) {
$inDegree[$dependent]--;
if ($inDegree[$dependent] == 0) {
array_push($queue, $dependent);
}
}
}
}
return $result;
}
// Example usage:
$recipes = ["bread", "sandwich", "burger"];
$ingredients = [["yeast", "flour"], ["bread", "meat"], ["sandwich", "meat", "bread"]];
$supplies = ["yeast", "flour", "meat"];
print_r(findAllRecipes($recipes, $ingredients, $supplies));
?> Explanation:
This approach efficiently handles dependencies and ensures that all possible recipes are processed in the correct order, leveraging topological sorting to manage complex interdependencies. |
Beta Was this translation helpful? Give feedback.
We need to determine all possible recipes that can be created given a set of initial supplies and recipes that may depend on each other. The key challenge is to handle dependencies between recipes and ensure that all required ingredients (either from initial supplies or other recipes) are available.
Approach