Problem 219999 · medium · Phase 02 Linear Data Structures

Fulfil Orders From Stock

hash maps · counting · resource check · simulation

A shop has stock, a dict mapping item name to quantity on hand. Orders arrive in sequence: orders[i] is a list of item names, one entry per unit wanted (an item may be listed several times). An order is fulfilled only if every requested unit is available; the units are then removed from stock. Otherwise the order is rejected and stock is unchanged. Return a list of booleans, one per order. Do not modify the stock dict you were given.

Examples

Input:  stock = {"apple": 2, "pear": 1}
        orders = [["apple", "pear"], ["apple", "apple"], ["apple"]]
Output: [True, False, True]
Explanation: after order 0 one apple is left; order 1 wants two, so it is rejected; order 2 takes the last apple.

Constraints

  • 0 <= len(orders) <= 10**4, total units across all orders <= 10**5
  • Target complexity: O(total units) time.

Goals

  • Check an all-or-nothing requirement against a count dict
  • Mutate a working copy of the state only after the check passes
Starting Python…