Product Total With the Cheapest Promotion
Learn this problemProblem statement
Given product and promotion records, return the minimum total price in integer cents.
Each product record has the form sku|name|quantity|priceCents. Skip a product record exactly when quantity or priceCents is negative, and continue processing later records. Every other record contributes independently, including repeated SKUs.
Promotion records have either form:
sku|pct|p: chargefloor(quantity * priceCents * (100 - p) / 100)cents for the full product record.sku|bxyf|x|y: each complete group ofx + yunits containsyfree units. The charged count isquantity - floor(quantity / (x + y)) * y.
A SKU has at most one promotion of each type. For every valid product record, choose the minimum of its regular price and every applicable promoted price. A product without a promotion uses its regular price.
Function
minimumProductTotal(products: String[], promotions: String[]) → longExamples
Example 1
products = ["A|Apple|3|100","B|Bread|2|250","C|Bad|-1|900"]promotions = ["A|pct|20","A|bxyf|2|1","B|pct|10"]return = 650For A, buy-two-get-one-free charges 200, cheaper than the 240 percentage price. B costs 450 after 10% off. The negative-quantity C record is skipped.
Example 2
products = ["X|Item|5|99","X|Again|2|99","Y|Free|0|500","Z|Skip|1|-5"]promotions = ["X|bxyf|2|1","Y|pct|100"]return = 594The two X records are priced independently at 396 and 198. Zero quantity contributes zero, and the negative-price record is skipped.
Example 3
products = ["A|A|1|101","B|B|7|80","C|C|2|200"]promotions = ["A|pct|33","B|bxyf|2|1"]return = 867The percentage price for A floors 67.67 to 67 cents. B charges five of seven units for 400, and unpromoted C costs 400.
Constraints
0 <= products.length <= 10^5and0 <= promotions.length <= 2 * 10^5.- Every record is syntactically valid, and string fields do not contain
|. - For percentage promotions,
0 <= p <= 100. - For buy-x-get-y-free promotions,
xandyare positive. - Every intermediate line price and the final answer fit in a signed 64-bit integer.