Skip to main content

Cakes Fulfillment Stock

easy
Software engineer

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

3

Explanation

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

1

Explanation

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 == 3
  • 0 <= stock[i] <= 10⁹
  • 1 <= orders.length <= 10⁵
  • orders[i].length == 3
  • 0 <= orders[i][j] <= 10⁹

OA

ArraySimulation
Language
Code editor loads in the browser.

Output

Input

[3,3,3]
[[1,0,1],[0,2,0],[1,1,1],[2,2,2]]

Expected

3

Your output

Run to see your output.