Stacked Item and Category Discounts
Problem statement
You are given a shopping cart and item-specific or category-wide percentage discounts. Each cart item has a name, an integer-cent price, and a category. Each discount has type item or category, a matching name, and a percentage.
For each item, apply its matching item discount first and its matching category discount second. The two discounts stack multiplicatively. For example, 10% off followed by 20% off leaves price * 0.90 * 0.80, so the total discount is 28%.
The total discount on one item cannot exceed 80% of its original price. If the next discount would exceed that limit, apply only enough of it to reach exactly 80%. Ignore unmatched discounts.
Return a three-element array containing the subtotal, total rounded discount, and final total. Compute each item's combined discount without intermediate rounding, cap it at 80%, and then round that final discount to the nearest cent. An exact half-cent tie rounds to the nearest even integer.
Function
calculateStackedDiscountTotals(itemNames: String[], prices: int[], categories: String[], discountTypes: String[], discountNames: String[], percentOff: int[]) → long[]Examples
Example 1
itemNames = ["Milk"]prices = [500]categories = ["Dairy"]discountTypes = ["item", "category"]discountNames = ["Milk", "Dairy"]percentOff = [10, 20]return = [500, 140, 360]The stacked price factor is 0.90 * 0.80 = 0.72, so the discount is 140 cents.
Example 2
itemNames = ["Laptop"]prices = [1000]categories = ["Electronics"]discountTypes = ["item", "category"]discountNames = ["Laptop", "Electronics"]percentOff = [70, 50]return = [1000, 800, 200]The discounts would save 85%, so the per-item cap limits the discount to 800 cents.
Example 3
itemNames = ["A"]prices = [105]categories = ["X"]discountTypes = ["item"]discountNames = ["A"]percentOff = [10]return = [105, 10, 95]The raw 10.5-cent discount is an exact tie and rounds to the even value 10.
Constraints
1 ≤ itemNames.length = prices.length = categories.length ≤ 100000.0 ≤ discountTypes.length = discountNames.length = percentOff.length ≤ 100000.0 ≤ prices[i] ≤ 10^9.0 ≤ percentOff[i] ≤ 100.- Every discount type is exactly
itemorcategory. - There is at most one item discount per item name and at most one category discount per category.
- Names and categories are non-empty case-sensitive strings.
- The subtotal and returned totals fit in a signed 64-bit integer.