LeetCode 1357. Apply Discount Every n Orders
Question
There is a sale in a supermarket, there will be a discount
every n
customer.
There are some products in the supermarket where the id of the i-th
product is products[i]
and the price per unit of this product is prices[i]
.
The system will count the number of customers and when the n-th
customer arrive he/she will have a discount
on the bill. (i.e if the cost is x
the new cost is x - (discount * x) / 100
). Then the system will start counting customers again.
The customer orders a certain amount of each product where product[i]
is the id of the i-th
product the customer ordered and amount[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.
You can check the official leetcode website
Example
1 | Input |
Solution
It needs you to read more carefully, and we only need to keep tracking the relationship between productID and Price. Also, we need to pay attention when we give the low price (the value of n).
Code
1 | // time: O(n), space:O(n) |