Leecode #1357 每隔n个顾客打折

少于 1 分钟读完

超市里正在举行打折活动,每隔 n 个顾客会得到 discount 的折扣。

超市里有一些商品,第 i 种商品为 products[i] 且每件单品的价格为 prices[i] 。

结账系统会统计顾客的数目,每隔 n 个顾客结账时,该顾客的账单都会打折,折扣为 discount (也就是如果原本账单为 x ,那么实际金额会变成 x - (discount * x) / 100 ),然后系统会重新开始计数。

顾客会购买一些商品, product[i] 是顾客购买的第 i 种商品, amount[i] 是对应的购买该种商品的数目。

请你实现 Cashier 类:

Cashier(int n, int discount, int[] products, int[] prices) 初始化实例对象,参数分别为打折频率 n ,折扣大小 discount ,超市里的商品列表 products 和它们的价格 prices 。 double getBill(int[] product, int[] amount) 返回账单的实际金额(如果有打折,请返回打折后的结果)。返回结果与标准答案误差在 10^-5 以内都视为正确结果。

class Cashier:

    def __init__(self, n: int, discount: int, products: List[int], prices: List[int]):
        import collections
        self.n = n
        self.discount = discount
        self.prices_map = collections.defaultdict(int)
        for i in range(len(products)):
            self.prices_map[products[i]] = prices[i]

        self.count = 0

    def getBill(self, product: List[int], amount: List[int]) -> float:
        bill = sum(map(lambda x: self.prices_map[x[0]]*x[1], zip(product, amount)))
        self.count += 1
        if self.count == self.n:
            self.count = 0
            return bill * (1-self.discount/100)
        else:
            return bill


# Your Cashier object will be instantiated and called as such:
# obj = Cashier(n, discount, products, prices)
# param_1 = obj.getBill(product,amount)

分类:

更新时间: