Warehouse Stock Management
Flipkart's warehouses are vast, and efficient inventory management is crucial for fulfilling customer orders quickly. Products are stored in a highly optimized linear arrangement across many storage slots. To maintain stock levels and plan logistics, it's essential to quickly ascertain stock availability across various sections of the warehouse.
Implement a data structure that efficiently handles two types of operations on an array representing stock levels in contiguous warehouse slots:
You will be given an initial array of stock counts. Your implementation should support subsequent update and query operations.
Your solution should implement a class WarehouseManager with the following methods:
constructor(initialStocks: number[]): Initializes the system with an array initialStocks where initialStocks[i] represents the stock count in slot i.updateStock(slotIndex: number, newStock: number): void: Updates the stock count in slotIndex to newStock.queryMaxStock(startIndex: number, endIndex: number): number: Returns the maximum stock count within the range of slots from startIndex to endIndex (inclusive).newStock will always be non-negative.startIndex and endIndex will always be valid and startIndex <= endIndex.Examples
Example 1
Input:
operations = ["WarehouseManager","queryMaxStock","updateStock","queryMaxStock","queryMaxStock"], params = [[[10,20,5,30,15]],[0,4],[2,25],[1,3],[0,1]]
Output:
[null,30,null,30,20]
Note:
Initialize with [10,20,5,30,15]. Max in [0,4] is 30. Update slot 2 to 25. Array becomes [10,20,25,30,15]. Max in [1,3] is 30. Max in [0,1] is 20.
Example 2
Input:
operations = ["WarehouseManager","queryMaxStock","updateStock","queryMaxStock","queryMaxStock","updateStock","queryMaxStock"], params = [[[1,2,3,4,5]],[0,0],[0,100],[0,0],[2,4],[4,0],[0,4]]
Output:
[null,1,null,100,5,null,100]
Note:
Initialize with [1,2,3,4,5]. Max in [0,0] is 1. Update slot 0 to 100. Array becomes [100,2,3,4,5]. Max in [0,0] is 100. Max in [2,4] is 5. Update slot 4 to 0. Array becomes [100,2,3,4,0]. Max in [0,4] is 100.
Example 3
Input:
operations = ["WarehouseManager","queryMaxStock","updateStock","queryMaxStock"], params = [[[50]],[0,0],[0,10],[0,0]]
Output:
[null,50,null,10]
Note:
Initialize with [50]. Max in [0,0] is 50. Update slot 0 to 10. Array becomes [10]. Max in [0,0] is 10.
Constraints
1 <= initialStocks.length <= 10^50 <= initialStocks[i] <= 10^90 <= slotIndex < initialStocks.length0 <= newStock <= 10^90 <= startIndex <= endIndex < initialStocks.lengthupdateStock and queryMaxStock calls will not exceed 10^5.updateStock and queryMaxStock should be O(log N), where N is the number of slots.O(N).updateStock and queryMaxStock methods in terms of time and space complexity.Loading editor...