Skip to content

Commit e73ebd3

Browse files
authored
Create apply-discount-every-n-orders.cpp
1 parent efe2d9e commit e73ebd3

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

C++/apply-discount-every-n-orders.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Time: ctor: O(m), m is the number of all products
2+
// getBill: O(p), p is the number of products to bill
3+
// Space: O(m)
4+
5+
class Cashier {
6+
public:
7+
Cashier(int n, int discount, vector<int>& products, vector<int>& prices)
8+
: n_{n}
9+
, discount_{discount}
10+
, curr_{0}
11+
{
12+
for (int i = 0; i < products.size(); ++i) {
13+
lookup_[products[i]] = prices[i];
14+
}
15+
}
16+
17+
double getBill(vector<int> product, vector<int> amount) {
18+
curr_ = (curr_ + 1) % n_;
19+
double result = 0.0;
20+
for (int i = 0; i < product.size(); ++i) {
21+
result += lookup_[product[i]] * amount[i];
22+
}
23+
return curr_ == 0 ? result * (1.0 - discount_ / 100.0) : result;
24+
}
25+
26+
private:
27+
int n_;
28+
int discount_;
29+
int curr_;
30+
unordered_map<int, int> lookup_;
31+
};

0 commit comments

Comments
 (0)