Ctrl K

Warehouse Top Stock

Medium
A warehouse keeps a log of operations. Operation i is the pair items[i] and changes[i]: units of item code items[i] are either delivered (changes[i] > 0 of them) or shipped out (-changes[i] of them when changes[i] < 0). The warehouse starts empty. After every operation you need to know how many units the best-stocked item has. Return an array of the same length where entry i is the largest stock of a single item after operation i, or 0 if the warehouse is empty at that moment. Shipments never exceed what is on hand, so no item's stock ever goes negative.
Example 1
items=[4, 9, 4, 9], changes=[5, 3, -4, 1] → [5, 5, 3, 4]

The stock goes {4: 5} → {4: 5, 9: 3} → {4: 1, 9: 3} → {4: 1, 9: 4}, so the largest counts are 5, 5, 3, 4.

Example 2
items=[7, 7, 2], changes=[2, -2, 6] → [2, 0, 6]

After the second step the warehouse is empty, so that entry is 0.

Example 3
items=[1, 2, 3], changes=[1, 1, 1] → [1, 1, 1]
Constraints
  • 1 ≤ len(items) = len(changes) ≤ 10⁵
  • 1 ≤ items[i] ≤ 10⁵
  • -10⁵ ≤ changes[i] ≤ 10⁵, changes[i] ≠ 0
  • No item's stock is ever negative
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMost Common Values MediumJobs With a Cooldown MediumSorted Into Runs of Three MediumMost Searched Words MediumSpread Out the Labels Medium
loading Python…
Input
items=[4, 9, 4, 9], changes=[5, 3, -4, 1]
Expected
[5, 5, 3, 4]

Run checks the first 3 tests in your browser. Submit checks all 51 tests on the server, hidden ones included.