TopcoderArchive
Archive
Algorithm problem

Servings

Problem statement, definition, constraints, and public examples.

Problem Statement

A food catering operation would like to determine how many people can be served a certain dish given the amount of each ingredient that's currently in stock. You are given the recipe for the dish 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 recipe yields. You are also given a int[], stock, where the ith element represents the number of units of ingredient i (the ith element of recipe) in stock. Determine the maximum number of whole servings that can be produced.

Definition

Class:
Servings
Method:
maxServings
Parameters:
String[], int, int[]
Returns:
int
Method signature:
int maxServings(String[] recipe, int servings, int[] stock)
(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 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.
  • stock contains exactly the same number of elements as recipe.
  • Each element of stock is between 0 and 1000, inclusive.

Examples

  1. {"10 FISH", "5 LIME", "4 OIL", "9 CHIVES", "9 GINGER"}
    10
    {90, 65, 30, 90, 44}
    Returns: 48
    To produce 48 servings, we require 48 FISH, 24 LIME, 19.2 OIL, 43.2 CHIVES, and 43.2 GINGER. We cannot produce any more than 48 servings since we are limited by our stock of GINGER.
  2. {"1 SPINACH", "2 SAUSAGE", "3 GARLIC", "4 PARMESAN", "5 OREGANO",
     "6 BELLPEPPER", "7 ONION", "8 SALT", "9 RICOTTA", "10 BREAD"}
    100
    {9, 6, 8, 13, 19, 1000, 44, 55, 77, 300}
    Returns: 266
  3. {"10 A", "5 B", "7 Y", "3 Z"}
    51
    {404, 198, 299, 119}
    Returns: 2019
  4. {"8 RHUBARB"}
    93
    {176}
    Returns: 2045
    With 176 units of RHUBARB, you can make the recipe 22 times over and 22 * 93 = 2046.
Back to all problems