Apply Discount Every n Orders
Solution Code
class Cashier:
def __init__(
self, n: int, discount: int, products: List[int], prices: List[int]
):
self.price = dict()
for product, price in zip(products, prices):
self.price[product] = price
self.n = n
self.discount = discount
self.custom_id = 0
def getBill(self, product: List[int], amount: List[int]) -> float:
self.custom_id += 1
payment = 0.0
for k, v in zip(product, amount):
payment += self.price[k] * v
if self.custom_id % self.n == 0:
payment -= payment * self.discount / 100
return paymentTime Complexity
O(P) for initialization; O(M) per getBill() call
Space Complexity
O(P) for initialization; O(1) auxiliary space per getBill() call

