TopcoderArchive
Archive
Algorithm problem

FoodServings

Problem statement, definition, constraints, and public examples.

Problem Statement

A food catering operation would like to determine how many servings of a dish it can produce given a strict budget. 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 ingredient i (the ith element of recipe) in dollars, and an int, budget, that represents the total ingredient budget in dollars, determine the maximum number of whole servings that can be produced.

Definition

Class:
FoodServings
Method:
maxServings
Parameters:
int[], int, int[], int
Returns:
int
Method signature:
int maxServings(int[] recipe, int servings, int[] prices, int budget)
(be sure your method is public)

Notes

  • The number of servings you make need not be an integral multiple of servings. For example, you can make servings/2 servings by using half as much of each ingredient.

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.
  • budget is between 100 and 10000, inclusive.
  • An increase or decrease of the budget by 1 will not affect the number of servings returned.

Examples

  1. {10, 5, 4, 9, 9}
    10
    {9, 2, 1, 1, 2}
    274
    Returns: 20
    Producing 20 servings would require 20 units of ingredient 0, 10 units of ingredient 1, 8 units of ingredient 2, 18 units of ingredient 3, and 18 units of ingredient 4. The total cost would then be 20 * 9 + 10 * 2 + 8 * 1 + 18 * 1 + 18 * 2 = 262. Any more servings would exceed the budget of 274.
  2. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    100
    {50, 45, 40, 35, 30, 25, 20, 15, 10, 5}
    10000
    Returns: 909
  3. {10, 5, 7, 3}
    10
    {50, 40, 3, 1}
    100
    Returns: 1
  4. {10, 10, 10}
    10
    {50, 50, 50}
    100
    Returns: 0
Back to all problems