Problem · Hash Table

Robot Inventory Tracking

Learn this problem
MediumAirbnbNEW GRADOA

Problem statement

Implement an inventory tracking system for a robot retail store. You are given a transaction log logs. Process every record in order.

Each record has one of these formats:

  • ["supply", robotName, count, price]: add count units of robotName to inventory at unit price price.
  • ["sell", robotName, count]: sell count units of robotName. If units are available at different prices, sell the cheapest units first.
  • ["upgrade", robotName, count, oldPrice, newPrice]: move count units of robotName from oldPrice to the higher price newPrice.

The count and price fields are decimal strings. Return an integer array containing the revenue from each sell transaction in the order those transactions appear.

A solution with time complexity no worse than O(logs.length^2) fits within the execution time limit.

Function

trackRobotInventory(logs: String[][]) → int[]

Examples

Example 1

logs = [["supply","robot1","2","100"],["supply","robot2","3","60"],["sell","robot1","1"],["sell","robot2","1"],["upgrade","robot2","1","60","100"],["sell","robot2","1"],["sell","robot2","1"]]return = [100,60,60,100]
  1. The two supply records add two robot1 units at price 100 and three robot2 units at price 60.
  2. Selling one robot1 produces revenue 100.
  3. Selling one robot2 produces revenue 60.
  4. The upgrade record moves one remaining robot2 unit from price 60 to price 100.
  5. The next robot2 sale uses the remaining price-60 unit first, producing revenue 60.
  6. The final robot2 sale uses the price-100 unit, producing revenue 100.

The revenues are therefore [100,60,60,100].

Constraints

  • Every sell transaction can be fulfilled by the current inventory.
  • Every upgrade transaction has at least count matching units at oldPrice.
  • For every upgrade transaction, newPrice is higher than oldPrice.

More Airbnb problems

drafts saved locally
public int[] trackRobotInventory(String[][] logs) {
    // write your code here
}
logs[["supply","robot1","2","100"],["supply","robot2","3","60"],["sell","robot1","1"],["sell","robot2","1"],["upgrade","robot2","1","60","100"],["sell","robot2","1"],["sell","robot2","1"]]
expected[100,60,60,100]
checking account