Inventory Discount Tracker
Learn this problemProblem statement
Implement a simplified inventory tracker for a large retail store. You are given a price list pricelist that describes the current market price for each item and a chronological transaction log logs. Process all transactions and return the total revenue from all sales.
Price list
Each price-list entry has the format "<item_name>: <price>", which means that item_name has the regular unit price price.
Transactions
Each log entry has one of these formats:
"sell <item_name>, <count>": sellcountunits ofitem_name."discount_start <item_name>, <discount_amount>, <max_count>": start a discount foritem_name. During this discount, at mostmax_countunits in total are sold forprice - discount_amount. Any additional units are sold for the regular price. The discounted quota is consumed across sales until the discount ends."discount_end <item_name>": end the active discount foritem_name. The item is guaranteed to have an active discount at this point.
There is at most one active discount for each item at any time. A discount remains active after its quota is exhausted, but all later units use the regular price until discount_end.
Return the total revenue as an integer.
A solution with time complexity no worse than O(logs.length^2 * pricelist.length) fits within the execution limit.
Function
solution(pricelist: String[], logs: String[]) → intExamples
Example 1
pricelist = ["item1: 100","item2: 200"]logs = ["sell item1, 1","sell item1, 2","sell item2, 2","discount_start item2, 40, 1","sell item2, 2","sell item1, 1","discount_end item2","sell item2, 1"]return = 1360Process the sales in order. The first three sales contribute 100, 200, and 400. The discount on item2 applies to one unit of its next two-unit sale, so that sale contributes 160 + 200 = 360. The final two sales contribute 100 and 200. Therefore the total is 100 + 200 + 400 + 360 + 100 + 200 = 1360.
Constraints
1 ≤ pricelist.length ≤ 100- Each element of
pricelisthas the format"<item_name>: <price>", whereitem_namecontains only alphanumeric characters andpriceis a positive integer. - All item names in
pricelistare unique. 1 ≤ logs.length ≤ 1000- For every
sellentry,1 ≤ count ≤ 1000. - For every
discount_startentry,discount_amountis a positive integer and1 ≤ max_count ≤ 100. - Every item referenced in
logsexists inpricelist. - There is at most one active discount per item. A
discount_startis issued only when that item has no active discount, and adiscount_endis issued only when it has one. - All entries in
logsare sorted chronologically. - The total revenue fits in a signed 32-bit integer.