Problem Statement
A food catering operation would like to adapt an existing recipe to serve a larger number of people. You are given the existing recipe as a String[], recipe, where each element is formatted as "AMOUNT NAME" (quotes for clarity only). NAME is the name of the ingredient, and AMOUNT is an integer representing the number of units required of that ingredient. You are given an int, servings, that represents the number of servings that the existing recipe yields. You are also given an int, targetServings, that represents the number of servings that must be produced. Determine the minimum number of integral units of each ingredient required to yield the target number of servings. Return a int[], where the ith element represents the number of units required of the ith ingredient in recipe.
Definition
- Class:
- IngredientList
- Method:
- ingredients
- Parameters:
- String[], int, int
- Returns:
- int[]
- Method signature:
- int[] ingredients(String[] recipe, int servings, int targetServings)
- (be sure your method is public)
Constraints
- recipe contains between 1 and 10 elements, inclusive.
- Each element of recipe is formatted as "AMOUNT NAME" (quotes for clarity only).
- Each AMOUNT is an integer between 1 and 10, inclusive, with no leading zeros.
- Each NAME contains between 1 and 10 uppercase letters ('A'-'Z'), inclusive.
- There will be no duplicate NAMEs in recipe.
- servings is between 10 and 100, inclusive.
- targetServings is between 20 and 2000, inclusive.
- targetServings is greater than servings.
Examples
{"10 FISH", "5 LIME", "4 OIL", "9 CHIVES", "9 GINGER"}10
25
Returns: { 25, 13, 10, 23, 23 }The original recipe serves 10 people. To serve 25, we need 25 FISH, 12.5 LIME, 10 OIL, 22.5 CHIVES, and 22.5 GINGER. We can only buy integral units of each ingredient, so the return value is {25, 13, 10, 23, 23}.{"1 SPINACH", "2 SAUSAGE", "3 GARLIC", "4 PARMESAN", "5 OREGANO", "6 BELLPEPPER", "7 ONION", "8 SALT", "9 RICOTTA", "10 BREAD"}100
122
Returns: { 2, 3, 4, 5, 7, 8, 9, 10, 11, 13 }{"10 A", "5 B", "7 Y", "3 Z"}51
2000
Returns: { 393, 197, 275, 118 }