Skip to content

imvickykumar999/Inventory-Level

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Inventory Management

Problem Statement

You are given two integer arrays:

  • products, where products[i] represents the product ID being updated at step i

  • changes, where changes[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

Function Signature

def inventoryManagement(products: List[int], changes: List[int]) -> List[int]:

Input

  • products: integer array of length n
  • changes: integer array of length n

Output

  • Return an integer array inventory_status of length n

Example 1

Input

products = [101, 104, 101, 105]
changes = [4, 2, -4, 3]

Output

[4, 4, 2, 3]

Explanation

  • After update 0: Product 101 has 4 units → max stock = 4
  • After update 1: Product 101 has 4, product 104 has 2 → max stock = 4
  • After update 2: Product 101 removed completely, product 104 has 2 → max stock = 2
  • After update 3: Product 105 has 3, product 104 has 2 → max stock = 3

Example 2

Input

products = [150, 150, 120]
changes = [5, -5, 2]

Output

[5, 0, 2]

Explanation

  • After update 0: Product 150 has 5 units → max stock = 5
  • After update 1: Product 150 removed completely → inventory empty → 0
  • After update 2: Product 120 has 2 units → max stock = 2

Constraints

  • The stock level of any product will never become negative.

Efficient Approach Hint 🚀

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

About

This problem asks you to track the inventory level of various products across multiple updates and return the quantity of the most stocked product after each change.

Topics

Resources

License

Stars

Watchers

Forks

Contributors