Apply Discount Every n Orders LeetCode solution in C++

1
class Cashier {
2
public:
3
int n;
4
int dis;
5
unordered_map<int, int> price;
6
int idx = 0;
7
Cashier(int n, int discount, vector<int> &products, vector<int> &prices) {
8
this->n = n;
9
this->dis = discount;
10
for (int i = 0; i < products.size(); i++) {
11
price[products[i]] = prices[i];
12
}
13
}
14

15
double getBill(vector<int> product, vector<int> amount) {
16
double res = 0;
17
idx++;
18
for (int i = 0; i < product.size(); i++) {
19
res += (price[product[i]] * amount[i]);
20
}
21
if (idx % n == 0) {
22
return (res * (100 - dis)) / 100;
23
}
24
return res;
25
}
26
};
27

28
/**
29
* Your Cashier object will be instantiated and called as such:
30
* Cashier* obj = new Cashier(n, discount, products, prices);
31
* double param_1 = obj->getBill(product,amount);
32
*/

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0