Cakes Fulfillment Stock
You have three types of cakes in stock, represented by an array stock of length 3. You receive multiple orders, where each order is also an array of three integers representing the required quantities of each cake type.
An order is fully completed only if the stock has enough cakes for all requested types.
Important Rule: Regardless of whether the order is fully completed or not, you must deduct from your stock as much as possible for each cake type in the order. This means for each cake type $i$, the new stock will be $\max(0, \text{stock}[i] - \text{order}[i])$.
Your task is to determine how many orders are fully completed under these conditions. You must process orders in the given order and may not skip or reorder them.
Example 1
Input
stock = [3, 3, 3], orders = [[1, 0, 1], [0, 2, 0], [1, 1, 1], [2, 2, 2]]Output
3Explanation
Order 1: [1, 0, 1]. Stock [3, 3, 3] is sufficient (3≥1, 3≥0, 3≥1). New stock: [2, 3, 2]. Order completed. Order 2: [0, 2, 0]. Stock [2, 3, 2] is sufficient (2≥0, 3≥2, 2≥0). New stock: [2, 1, 2]. Order completed. Order 3: [1, 1, 1]. Stock [2, 1, 2] is sufficient (2≥1, 1≥1, 2≥1). New stock: [1, 0, 1]. Order completed. Order 4: [2, 2, 2]. Stock [1, 0, 1] is insufficient. New stock: [0, 0, 0] (clamped at 0). Order not completed. Total: 3.
Example 2
Input
stock = [3, 2, 4], orders = [[2, 1, 2], [1, 2, 3], [1, 1, 1]]Output
1Explanation
After Order 1, stock becomes [1, 1, 2]. Order 2 requires [1, 2, 3], which is more than [1, 1, 2], so it fails. Stock becomes [0, 0, 0]. Order 3 fails.
Example 3
Input
stock = [3, 3, 3], orders = [[1, 2, 1], [2, 2, 2]]Output
1
Constraints
stock.length == 30 <= stock[i] <= 10⁹1 <= orders.length <= 10⁵orders[i].length == 30 <= orders[i][j] <= 10⁹
Cakes Fulfillment Stock
Overview
The problem asks us to simulate an inventory management process for three types of cakes. We are given an initial stock and a sequence of orders. The logic follows two specific rules:
- Completion Check: An order is "fully completed" only if we currently have enough of all three cake types to satisfy the entire order.
- Mandatory Deduction: Regardless of whether we had enough stock to "complete" the order, we must deduct the requested amounts from our stock. If an order asks for more than we have, the stock for that specific cake type simply drops to zero.
The goal is to count how many orders met the "fully completed" criteria. Since orders must be processed sequentially and the stock levels change after every single order, a straightforward simulation is the most effective approach.
Brute Force
Given that this problem involves a sequential simulation where each state depends on the previous one, the "brute force" approach is essentially the simulation itself. We iterate through the orders one by one, check the condition for completion, and update the stock levels.
def count_fulfilled_orders(stock: list[int], orders: list[list[int]]) -> int:
fulfilled_count = 0
for order in orders:
# Check if the order can be fully completed
can_complete = True
for i in range(3):
if stock[i] < order[i]:
can_complete = False
break
if can_complete:
fulfilled_count += 1
# Mandatory deduction: stock[i] = max(0, stock[i] - order[i])
for i in range(3):
stock[i] = max(0, stock[i] - order[i])
return fulfilled_count
- Time complexity: O(N) — We iterate through the list of $N$ orders exactly once, performing a constant number of operations (3) for each order.
- Space complexity: O(1) — We update the stock in-place and only use a single integer variable for the counter.
Optimal / Best Approach
The simulation used in the brute force is already optimal in terms of time complexity ($O(N)$), as we must examine every order at least once. We can refine the implementation for conciseness using Python's zip and all functions, though the underlying logic remains the same.
Note that since the stock is modified even for failed orders, we subtract the order from the stock and then "clamp" any negative values to zero using max(0, val).
def count_fulfilled_orders(stock: list[int], orders: list[list[int]]) -> int:
count = 0
for order in orders:
# 1. Check if all 3 types are sufficient BEFORE deducting
if all(stock[i] >= order[i] for i in range(3)):
count += 1
# 2. Deduct from stock as much as possible for every order
for i in range(3):
stock[i] = max(0, stock[i] - order[i])
return count
- Time complexity: O(N) — Where $N$ is the number of orders. We process each order in constant time because the number of cake types is fixed at 3.
- Space complexity: O(1) — We use a constant amount of extra space regardless of the input size (assuming the input
stockarray can be modified).
Edge Cases
- Zero Stock: If the initial stock is
[0, 0, 0], no order requesting a positive amount of cakes can be fulfilled, but the stock will remain[0, 0, 0]after deductions. - Zero Orders: If an order is
[0, 0, 0], it is technically fulfilled (sincestock[i] >= 0will always be true), and the stock remains unchanged. - Large Quantities: The cake counts can be up to $10^9$. Python handles arbitrarily large integers automatically, so we don't need to worry about 32-bit integer overflow.
- Empty Orders List: If
ordersis empty, the function correctly returns0.
Fast
Comments on the solution
No comments on the solution yet. Start a thread about the editorial or your own approach.
No comments yet for Cakes Fulfillment Stock. Be the first to start the thread.
No submissions recorded for this problem yet. Use Submit in the editor — each judge run is stored here.
Output
Input
Expected
Your output
Run to see your output.