FunctionRacerProblemsRace

Unfilled Orders

BeginnerHash mapsPython, JavaScript, TypeScript, Java, C++

stock maps product names to the number of units on the shelf. orders is a list of product names, one unit per order, handled from first to last.

An order is filled when its product is in stock with at least one unit left, and filling it takes one unit off the shelf. Otherwise the order cannot be filled; a product that is not a key of stock has zero units. Return how many orders could not be filled. You may modify stock.

Example 1

stock {"apple":2,"pear":1}
orders ["apple","pear","pear","apple","apple"]
returns 2

The two apples and the one pear are filled in order; the second pear and the third apple find nothing left.

Example 2

stock {"milk":1}
orders ["bread","milk"]
returns 1

There is no bread at all; the milk order is filled.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Hash maps problems