TopcoderArchive
Archive
Algorithm problem

SingleServing

Problem statement, definition, constraints, and public examples.

Problem Statement

You would like to make a single serving of a recipe designed to yield several servings. You are given the 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 recipe yields. Determine how much of each ingredient is required to make a single serving. Return a int[], where each the ith element represents the minimum number of whole units required of ingredient i (the ith element of recipe).

Definition

Class:
SingleServing
Method:
ingredients
Parameters:
String[], int
Returns:
int[]
Method signature:
int[] ingredients(String[] recipe, int servings)
(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 20, inclusive, with no leading zeros.
  • Each NAME contains between 1 and 10 uppercase letters ('A'-'Z'), inclusive.
  • servings is between 2 and 50, inclusive.

Examples

  1. {"10 FISH", "5 LIME", "4 OIL", "9 CHIVES", "9 GINGER"}
    3
    Returns: { 4,  2,  2,  3,  3 }
    To make a single serving, we need only 1/3 of the original amounts - approximately 3.33 FISH, 1.67 LIME, 1.33 OIL, 3 CHIVES, and 3 GINGER. The solution should contain the minimum number of whole units required of each ingredient, so the return value is {4, 2, 2, 3, 3}.
  2. {"1 SPINACH", "4 SAUSAGE", "8 GARLIC", "12 PARMESAN", "16 OREGANO",
     "20 BELLPEPPER", "16 ONION", "12 SALT", "16 RICOTTA", "20 BREAD"}
    15
    Returns: { 1,  1,  1,  1,  2,  2,  2,  1,  2,  2 }
  3. {"10 A", "5 B", "7 Y", "3 Z"}
    50
    Returns: { 1,  1,  1,  1 }
Back to all problems