1226. Recipe Batches

Easy1.5 points estimatedHash TableStringMaths3 hints

A bakery tracks its stock in a map pantry from ingredient name to units on hand. A map recipe gives, for one batch, how many units of each ingredient are needed.

Return the largest whole number of batches b that can be baked: for every ingredient in recipe, the pantry must hold at least b * recipe[ingredient] units. An ingredient that is missing from pantry counts as 0 units. Ingredients in the pantry that the recipe does not use are ignored. The result is 0 when not even one batch is possible.

Compute, for each recipe ingredient, the pantry amount divided by the need (rounded down), and take the minimum. This runs in O(m) time, where m is the size of the recipe.

Example 1

Input:
pantry = {"flour":500,"egg":7,"milk":10}recipe = {"flour":200,"egg":2,"milk":3}
Output:
2
Explanation:

Flour allows 2 batches, eggs 3 and milk 3, so the minimum is 2.

Example 2

Input:
pantry = {"salt":5}, recipe = {"salt":2,"pepper":1}
Output:
0
Explanation:

Pepper is missing from the pantry, so no batch can be baked.

Example 3

Input:
pantry = {"a":1000000000}, recipe = {"a":1}
Output:
1000000000
Explanation:

The only ingredient allows 1000000000 batches.

Constraints

1 ≤ recipe.size, pantry.size ≤ 100

1 ≤ recipe values ≤ 104

0 ≤ pantry values ≤ 109

Keys are lowercase words.

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(m)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…