Discounted Price Carts
MediumData Processing · Atlassian
You are given a product catalog and a list of shopping carts.
- Each product is
[sku, price_cents, tiers], where each tier is[min_quantity, percent_off]. - Each cart is a list of
[sku, quantity]rows. The same sku may appear in several rows of one cart.
To price a cart, first add up the quantity of each sku across all of its rows. For a sku with total quantity q:
- Among the tiers with
min_quantity <= q, use the one with the largestpercent_off. If no tier qualifies, the discount is0. - The discounted unit price is
price_cents * (100 - percent_off) // 100, rounded down to a whole cent. - The line cost is the discounted unit price times
q.
The cart total is the sum of its line costs. Discounts never carry over between carts. Return a list with the total, in cents, of each cart in order.
Example 1
- Input:
- products = [["MUG", 1250, [[2, 10], [5, 25]]], ["PEN", 199, []]], carts = [[["MUG", 3], ["PEN", 2]], [["MUG", 5]], [["MUG", 1], ["MUG", 1]]]
- Output:
- [3773, 4685, 2250]
- Explanation
- Cart 0: 3 mugs qualify for the 10% tier, so each costs
1250 * 90 // 100 = 1125and three cost 3375; the pens have no tiers and cost 398, giving 3773. Cart 1: 5 mugs reach the 25% tier,1250 * 75 // 100 = 937each, so 4685. Cart 2: the two rows of 1 mug combine to 2, which unlocks 10%, so 2250.
Example 2
- Input:
- products = [["CAP", 999, [[3, 15]]]], carts = [[["CAP", 2]], [["CAP", 3]]]
- Output:
- [1998, 2547]
- Explanation
- Two caps miss the 3-unit tier and cost full price, 1998. Three caps get 15% off,
999 * 85 // 100 = 849each, so 2547.
Example 3
- Input:
- products = [["TEE", 2000, [[1, 5]]]], carts = [[]]
- Output:
- [0]
- Explanation
- An empty cart costs nothing.
Constraints
1 <= products.length <= 10^4, skus are distinct- Each product has at most 10 tiers, with
1 <= min_quantity <= 10^4and0 <= percent_off <= 100 1 <= price_cents <= 10^60 <= carts.length <= 10^4, with at most10^5rows across all carts1 <= quantity <= 10^4, and every sku in a cart exists inproducts