Inventory Reservations
MediumSimulation · Walmart
A store's starting inventory is given as stock, a dict mapping SKU to available quantity. Orders arrive as reservations, a list of [order_id, items], where items is a list of [sku, qty] lines.
Process reservations in list order. Each reservation is all-or-nothing:
- First combine lines for the same SKU within the order (two lines of
["milk", 1]need 2 milk). - The reservation is accepted only if, for every SKU it needs, the remaining quantity is at least the combined amount. A SKU missing from
stockhas quantity0. - An accepted reservation subtracts its quantities from the remaining stock. A rejected one changes nothing.
Return [accepted, final_stock], where accepted is a list of booleans (one per reservation, in order) and final_stock is a list of [sku, remaining_qty] rows for every SKU in the starting stock, sorted by SKU ascending.
Example 1
- Input:
- stock = {"apple": 5, "milk": 2}, reservations = [[101, [["apple", 2], ["milk", 1]]], [102, [["milk", 1], ["milk", 1]]], [103, [["apple", 3]]]]
- Output:
- [[True, False, True], [["apple", 0], ["milk", 1]]]
- Explanation
- Order 101 fits and leaves 3 apples and 1 milk. Order 102 needs 2 milk in total but only 1 is left, so it is rejected as a whole. Order 103 takes the last 3 apples.
Example 2
- Input:
- stock = {"tea": 4}, reservations = [[1, [["tea", 1], ["jam", 1]]], [2, [["tea", 4]]]]
- Output:
- [[False, True], [["tea", 0]]]
- Explanation
jamis not stocked, so order 1 is rejected and its tea is not reserved. Order 2 can then take all 4 tea.
Example 3
- Input:
- stock = {"b": 1, "a": 3}, reservations = []
- Output:
- [[], [["a", 3], ["b", 1]]]
- Explanation
- Nothing is processed, so the stock is reported unchanged, sorted by SKU.
Constraints
0 <= reservations.length <= 10^50 <= stock[sku] <= 10^9, and SKUs are non-empty strings- Each order has
1 <= items.length <= 50lines with1 <= qty <= 10^6