【leetcode】1357. Apply Discount Every n Orders
题目如下:
There is a sale in a supermarket, there will be a
discounteveryncustomer.
There are some products in the supermarket where the id of thei-thproduct isproducts[i]and the price per unit of this product isprices[i].
The system will count the number of customers and when then-thcustomer arrive he/she will have adiscounton the bill. (i.e if the cost isxthe new cost isx - (discount * x) / 100). Then the system will start counting customers again.
The customer orders a certain amount of each product whereproduct[i]is the id of thei-thproduct the customer ordered andamount[i]is the number of units the customer ordered of that product.Implement the
Cashierclass:
Cashier(int n, int discount, int[] products, int[] prices)Initializes the object withn, thediscount, theproductsand theirprices.double getBill(int[] product, int[] amount)returns the value of the bill and apply the discount if needed. Answers within10^-5of the actual value will be accepted as correct.Example 1:
Input ["Cashier","getBill","getBill","getBill","getBill","getBill","getBill","getBill"] [[3,50,[1,2,3,4,5,6,7],[100,200,300,400,300,200,100]],[[1,2],[1,2]],[[3,7],[10,10]],[[1,2,3,4,5,6,7],[1,1,1,1,1,1,1]],[[4],[10]],[[7,3],[10,10]],[[7,5,3,1,6,4,2],[10,10,10,9,9,9,7]],[[2,3,5],[5,3,2]]] Output [null,500.0,4000.0,800.0,4000.0,4000.0,7350.0,2500.0] Explanation Cashier cashier = new Cashier(3,50,[1,2,3,4,5,6,7],[100,200,300,400,300,200,100]); cashier.getBill([1,2],[1,2]); // return 500.0, bill = 1 * 100 + 2 * 200 = 500. cashier.getBill([3,7],[10,10]); // return 4000.0 cashier.getBill([1,2,3,4,5,6,7],[1,1,1,1,1,1,1]); // return 800.0, The bill was 1600.0 but as this is the third customer, he has a discount of 50% which means his bill is only 1600 - 1600 * (50 / 100) = 800. cashier.getBill([4],[10]); // return 4000.0 cashier.getBill([7,3],[10,10]); // return 4000.0 cashier.getBill([7,5,3,1,6,4,2],[10,10,10,9,9,9,7]); // return 7350.0, Bill was 14700.0 but as the system counted three more customers, he will have a 50% discount and the bill becomes 7350.0 cashier.getBill([2,3,5],[5,3,2]); // return 2500.0Constraints:
1 <= n <= 10^40 <= discount <= 1001 <= products.length <= 2001 <= products[i] <= 200- There are not repeated elements in the array
products.prices.length == products.length1 <= prices[i] <= 10001 <= product.length <= products.lengthproduct[i]exists inproducts.amount.length == product.length1 <= amount[i] <= 1000- At most
1000calls will be made togetBill.- Answers within
10^-5of the actual value will be accepted as correct.
解题思路:这个题目没什么好说的吧。
代码如下:
class Cashier(object): def __init__(self, n, discount, products, prices): """ :type n: int :type discount: int :type products: List[int] :type prices: List[int] """ self.n = n self.discount = discount self.dic = {} for product,price in zip(products,prices): self.dic[product] = price self.inx = 1 def getBill(self, product, amount): """ :type product: List[int] :type amount: List[int] :rtype: float """ total = 0 for p,a in zip(product,amount): total += self.dic[p] * a if self.inx % self.n == 0: total = float(total) - float(total) * float(self.discount) / float(100) self.inx += 1 return total
浙公网安备 33010602011771号