Now add promotions to the checkout from the previous question. You are given the same items format and invalid-record rules, together with a promotions array.
Each promotion starts with the SKU to which it applies:
"<sku>|pct|<percent>"reduces that SKU's line price bypercentpercent."<sku>|bxyf|<x>|<y>"is a buy-X-get-Y-free offer. For everyxpaid units, up to the nextyunits of the same SKU are free. After thoseyunits, a new group begins.- For example, with a "buy-5-get-3-free" offer, buying
5through8units costs the price of5units, while a9thunit starts the next group.
- For example, with a "buy-5-get-3-free" offer, buying
For each valid product record, its final line price is the lowest price obtainable by either applying no promotion or applying exactly one promotion for its SKU. Promotions cannot be combined, and promotions for other SKUs do not apply
Return the sum of the chosen line prices as a string in the format "$<amount>", with exactly two digits after the decimal point. Round the final total to the nearest cent, with a half-cent rounded up.
Constraints:
- 0 ≤
promotions.length≤ 1000. - Every promotion has one of the two specified formats.
- 0 ≤
percent≤ 100. - 1 ≤
x,y≤ 10,000.
Example 1:
Input: items = ["a100|Apple|7|125", "b200|Banana|3|200"], promotions = ["a100|pct|20", "a100|bxyf|5|2", "b200|bxyf|5|2"] Output: "$12.25" Explanation: The percentage promotion makes the apple line $7.00, while the buy-five-get-two-free promotion makes it $6.25, so the latter is cheaper. The banana quantity is below the five-unit threshold, so no units are free, and it costs $6.00. The total is $12.25.
Example 2:
Input: items = ["a|Apple|2|150", "b|Banana|1|275"], promotions = ["a|pct|20"] Output: "$5.15"
Example 3:
Input: items = ["a|Apple|8|50"], promotions = ["a|bxyf|2|1"] Output: "$3.00"
