leetcode1357

 1 class Cashier:
 2     def __init__(self, n: int, discount: int, products: 'List[int]', prices: 'List[int]'):
 3         self.customer_num = 0
 4         self.dic = {}
 5         m = len(products)
 6         for i in range(m):
 7             self.dic[products[i]] = prices[i]
 8         self.n = n
 9         self.discount = discount
10 
11 
12     def getBill(self, product: 'List[int]', amount: 'List[int]') -> float:
13         self.customer_num += 1
14         self.customer_num %= self.n
15         m = len(product)
16         total_amount = 0
17         for i in range(m):
18             productid = product[i]
19             amt = amount[i]
20             total_amount += (self.dic[productid] * amt)
21         if self.customer_num == 0:
22             total_amount = total_amount - (self.discount * total_amount) / 100
23         return total_amount

算法思路:将products[]与prices[]建立映射字典,这样可以方便查询每一种商品对应的单价。

每增加一个用户,累加一次用户数量,并用n进行求模运算,每第n个用户就计算折扣。

猜你喜欢

转载自www.cnblogs.com/asenyang/p/12349488.html