Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/1357.html
1357. Apply Discount Every n Orders (Medium)
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.
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.0
Constraints:
1 <= n <= 10^4
0 <= discount <= 100
1 <= products.length <= 200
1 <= products[i] <= 200
- There are not repeated elements in the array
products
. prices.length == products.length
1 <= prices[i] <= 1000
1 <= product.length <= products.length
product[i]
exists inproducts
.amount.length == product.length
1 <= amount[i] <= 1000
- At most
1000
calls will be made togetBill
. - Answers within
10^-5
of the actual value will be accepted as correct.
Related Topics:
Design
Solution 1.
-
class Cashier { private int i; private int n; private int discount; private Map<Integer, Integer> d = new HashMap<>(); public Cashier(int n, int discount, int[] products, int[] prices) { this.n = n; this.discount = discount; for (int j = 0; j < products.length; ++j) { d.put(products[j], prices[j]); } } public double getBill(int[] product, int[] amount) { int dis = (++i) % n == 0 ? discount : 0; double ans = 0; for (int j = 0; j < product.length; ++j) { int p = product[j], a = amount[j]; int x = d.get(p) * a; ans += x - (dis * x) / 100.0; } return ans; } } /** * Your Cashier object will be instantiated and called as such: * Cashier obj = new Cashier(n, discount, products, prices); * double param_1 = obj.getBill(product,amount); */
-
class Cashier { public: Cashier(int n, int discount, vector<int>& products, vector<int>& prices) { this->n = n; this->discount = discount; for (int j = 0; j < products.size(); ++j) { d[products[j]] = prices[j]; } } double getBill(vector<int> product, vector<int> amount) { int dis = (++i) % n == 0 ? discount : 0; double ans = 0; for (int j = 0; j < product.size(); ++j) { int x = d[product[j]] * amount[j]; ans += x - (dis * x) / 100.0; } return ans; } private: int i = 0; int n; int discount; unordered_map<int, int> d; }; /** * Your Cashier object will be instantiated and called as such: * Cashier* obj = new Cashier(n, discount, products, prices); * double param_1 = obj->getBill(product,amount); */
-
class Cashier: def __init__(self, n: int, discount: int, products: List[int], prices: List[int]): self.i = 0 self.n = n self.discount = discount self.d = {product: price for product, price in zip(products, prices)} def getBill(self, product: List[int], amount: List[int]) -> float: self.i += 1 discount = self.discount if self.i % self.n == 0 else 0 ans = 0 for p, a in zip(product, amount): x = self.d[p] * a ans += x - (discount * x) / 100 return ans # Your Cashier object will be instantiated and called as such: # obj = Cashier(n, discount, products, prices) # param_1 = obj.getBill(product,amount)
-
type Cashier struct { i int n int discount int d map[int]int } func Constructor(n int, discount int, products []int, prices []int) Cashier { d := map[int]int{} for i, product := range products { d[product] = prices[i] } return Cashier{0, n, discount, d} } func (this *Cashier) GetBill(product []int, amount []int) (ans float64) { this.i++ dis := 0 if this.i%this.n == 0 { dis = this.discount } for j, p := range product { x := float64(this.d[p] * amount[j]) ans += x - (float64(dis)*x)/100.0 } return } /** * Your Cashier object will be instantiated and called as such: * obj := Constructor(n, discount, products, prices); * param_1 := obj.GetBill(product,amount); */