Member-only story
Leetcode 1357 Apply discount every n orders
In this Leetcode problem, we are asked to return the cost of different orders, knowing that we apply a discount every n
customers. Here is the problem statement:
There is a sale in a supermarket, there will be a
discount
everyn
customer.
There are some products in the supermarket where the id of thei-th
product isproducts[i]
and the price per unit of this product isprices[i]
.
The system will count the number of customers and when then-th
customer arrive he/she will have adiscount
on the bill. (i.e if the cost isx
the 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-th
product the customer ordered andamount[i]
is the number of units the customer ordered of that product.Implement the
Cashier
class:
Cashier(int n, int discount, int[] products, int[] prices)
Initializes the object withn
, thediscount
, theproducts
and theirprices
.
double getBill(int[] product, int[] amount)
returns the value of the bill and apply the discount if needed. Answers within10^-5
of the actual value will be accepted as correct.
Pay attention to the statement as it is quite long. Here are some possible inputs and constraints: