You are given two integer arrays:
-
products, whereproducts[i]represents the product ID being updated at stepi -
changes, wherechanges[i]represents the stock change for that product:- positive value → stock added
- negative value → stock removed
Return an integer array inventory_status of length n, where:
inventory_status[i]is the highest stock quantity among all products after the i-th update- If the inventory becomes empty, then
inventory_status[i] = 0
def inventoryManagement(products: List[int], changes: List[int]) -> List[int]:products: integer array of lengthnchanges: integer array of lengthn
- Return an integer array
inventory_statusof lengthn
products = [101, 104, 101, 105]
changes = [4, 2, -4, 3][4, 4, 2, 3]- After update 0: Product
101has4units → max stock =4 - After update 1: Product
101has4, product104has2→ max stock =4 - After update 2: Product
101removed completely, product104has2→ max stock =2 - After update 3: Product
105has3, product104has2→ max stock =3
products = [150, 150, 120]
changes = [5, -5, 2][5, 0, 2]- After update 0: Product
150has5units → max stock =5 - After update 1: Product
150removed completely → inventory empty →0 - After update 2: Product
120has2units → max stock =2
- The stock level of any product will never become negative.
Because n can be as large as 100,000, an efficient solution should use:
- HashMap / Dictionary → track stock of each product
- Max Heap / Counter optimization → quickly find current maximum stock
Target complexity:
- O(n log n) preferred
- O(n²) will timeout