BackmediumTwo Pointers Cred

Optimal Recipe Component Solution

Problem Statement

Given a list of ingredients available in a kitchen and a set of required components for a recipe, find the minimum length of a shopping list that includes all components from the recipe. If no such shopping list exists, return -1.

Example 1
Input
ingredients = ['egg', 'flour', 'oil', 'sugar', 'milk'], recipe = ['egg', 'flour', 'sugar']
Output
-1

Explanation: Step-by-step: We iterate through the recipe components. For each component, we check if it exists in the ingredients list. If a component is not found, we return -1. If all components are found, we return the length of the ingredients list.

Example 2
Input
ingredients = ['egg', 'flour', 'oil', 'sugar', 'milk'], recipe = ['egg', 'flour', 'sugar', 'milk']
Output
4

Explanation: Step-by-step: We iterate through the recipe components. For each component, we check if it exists in the ingredients list. If a component is not found, we return -1. If all components are found, we return the length of the ingredients list.

Constraints

  • 1 <= length of ingredients list <= 1000
  • 1 <= number of required components <= 100
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free