Find the smallest opening stock for a day

Write codeEasy10 min

Your task

A warehouse plans tomorrow's opening stock for one product. It knows the day's stock changes in order: deliveries add units and dispatches remove them.

Write min_opening_stock(changes) that returns the smallest opening stock that keeps the stock from going below zero.

Input and output:

  • changes: a list of 0 to 100,000 integers, each from -10,000 to 10,000. A positive change adds units and a negative one removes them.
  • Returns a non-negative integer.

Rules:

  • The stock starts at the opening value, then each change applies in order.
  • The stock after every change must be 0 or more. Reaching exactly 0 is fine.
  • The opening stock is at least 0, so a day that never dips below zero returns 0.
  • An empty list returns 0.

Examples

min_opening_stock(changes: list[int]) -> int
Example 1
Inputmin_opening_stock([-3, 2, -5])
Output6
Example 2
Inputmin_opening_stock([4, -6, 3, -2])
Output2