Problem Statement
A food catering operation would like to determine the total cost of serving a dish to a certain number of people. You are given the recipe for the dish as a int[], recipe, where each element represents the number of units required of a single ingredient. You are also given an int, servings, that represents the number of servings that the recipe yields. Given a int[], prices, where the ith element represents the price per unit of the ith ingredient in recipe in dollars, and an int, targetServings, that represents the number of servings that must be produced, determine the total cost in dollars of all the ingredients required. You can only buy integral units of each ingredient.
Definition
- Class:
- IngredientCost
- Method:
- cost
- Parameters:
- int[], int, int[], int
- Returns:
- int
- Method signature:
- int cost(int[] recipe, int servings, int[] prices, int targetServings)
- (be sure your method is public)
Constraints
- recipe contains between 1 and 10 elements, inclusive.
- Each element of recipe is between 1 and 10, inclusive.
- servings is between 10 and 100, inclusive.
- prices contains exactly the same number of elements as recipe.
- Each element of prices is between 1 and 50, inclusive.
- targetServings is between 20 and 2000, inclusive.
- targetServings is greater than servings.
Examples
{10, 5, 4, 9, 9}10
{9, 2, 1, 1, 2}25
Returns: 330
To produce 25 servings, we need 25 units of ingredient 0, 12.5 units of ingredient 1, 10 units of ingredient 2, 22.5 units of ingredient 3, and 22.5 units of ingredient 4. We can only buy integral units of each ingredient so the total cost is 25 * 9 + 13 * 2 + 10 * 1 + 23 * 1 + 23 * 2 = 330.{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}100
{50, 45, 40, 35, 30, 25, 20, 15, 10, 5}122
Returns: 1485
{10, 5, 7, 3}51
{2, 40, 3, 1}2000
Returns: 9609