Replay a day of stock movements
The warehouse log holds a day of movements for one part: positive for receipts, negative for picks. Replay them to get the closing figure.
- Stock can never go below zero. A pick larger than what is there empties the shelf and stops — it does not go negative.
- Because of that floor, the order matters: you cannot just add everything up.
stock_after_moves(opening: int, moves: list<int>) → int
Where you start
def stock_after_moves(opening: int, moves: list[int]) -> int:
Worked examples
| Call | Result |
|---|---|
stock_after_moves(10, [-3, 5, -4]) | 8 |
stock_after_moves(10, [-3, 5, -20]) | 0 |
stock_after_moves(5, [-10, 3]) | 3 |
stock_after_moves(0, []) | 0 |
Hint
Apply one movement at a time and clamp at zero after each.
Reference solution in Python
def stock_after_moves(opening: int, moves: list[int]) -> int:
level = max(0, opening)
for m in moves:
level = max(0, level + m)
return level