Skip to content

Commit cc9c7fd

Browse files
committed
feat: add solutions to lc problem: No.1357
No.1357.Apply Discount Every n Orders
1 parent 22009c6 commit cc9c7fd

File tree

6 files changed

+375
-2
lines changed

6 files changed

+375
-2
lines changed

solution/1300-1399/1357.Apply Discount Every n Orders/README.md

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,153 @@ cashier.getBill([2,3,5],[5,3,2]); // 返回 2500.0
6565

6666
<!-- 这里可写通用的实现逻辑 -->
6767

68+
**方法一:哈希表 + 模拟**
69+
70+
用哈希表 $d$ 存储商品编号和价格,然后遍历商品编号和数量,计算总价,再根据折扣计算折扣后的价格。
71+
72+
初始化的时间复杂度为 $O(n)$,其中 $n$ 为商品的数量。`getBill` 函数的时间复杂度为 $O(m)$,其中 $m$ 为购买商品的数量。空间复杂度为 $O(n)$。
73+
6874
<!-- tabs:start -->
6975

7076
### **Python3**
7177

7278
<!-- 这里可写当前语言的特殊实现逻辑 -->
7379

7480
```python
75-
81+
class Cashier:
82+
83+
def __init__(self, n: int, discount: int, products: List[int], prices: List[int]):
84+
self.i = 0
85+
self.n = n
86+
self.discount = discount
87+
self.d = {product: price for product, price in zip(products, prices)}
88+
89+
def getBill(self, product: List[int], amount: List[int]) -> float:
90+
self.i += 1
91+
discount = self.discount if self.i % self.n == 0 else 0
92+
ans = 0
93+
for p, a in zip(product, amount):
94+
x = self.d[p] * a
95+
ans += x - (discount * x) / 100
96+
return ans
97+
98+
99+
# Your Cashier object will be instantiated and called as such:
100+
# obj = Cashier(n, discount, products, prices)
101+
# param_1 = obj.getBill(product,amount)
76102
```
77103

78104
### **Java**
79105

80106
<!-- 这里可写当前语言的特殊实现逻辑 -->
81107

82108
```java
109+
class Cashier {
110+
private int i;
111+
private int n;
112+
private int discount;
113+
private Map<Integer, Integer> d = new HashMap<>();
114+
115+
public Cashier(int n, int discount, int[] products, int[] prices) {
116+
this.n = n;
117+
this.discount = discount;
118+
for (int j = 0; j < products.length; ++j) {
119+
d.put(products[j], prices[j]);
120+
}
121+
}
122+
123+
public double getBill(int[] product, int[] amount) {
124+
int dis = (++i) % n == 0 ? discount : 0;
125+
double ans = 0;
126+
for (int j = 0; j < product.length; ++j) {
127+
int p = product[j], a = amount[j];
128+
int x = d.get(p) * a;
129+
ans += x - (dis * x) / 100.0;
130+
}
131+
return ans;
132+
}
133+
}
134+
135+
/**
136+
* Your Cashier object will be instantiated and called as such:
137+
* Cashier obj = new Cashier(n, discount, products, prices);
138+
* double param_1 = obj.getBill(product,amount);
139+
*/
140+
```
141+
142+
### **C++**
143+
144+
```cpp
145+
class Cashier {
146+
public:
147+
Cashier(int n, int discount, vector<int>& products, vector<int>& prices) {
148+
this->n = n;
149+
this->discount = discount;
150+
for (int j = 0; j < products.size(); ++j) {
151+
d[products[j]] = prices[j];
152+
}
153+
}
154+
155+
double getBill(vector<int> product, vector<int> amount) {
156+
int dis = (++i) % n == 0 ? discount : 0;
157+
double ans = 0;
158+
for (int j = 0; j < product.size(); ++j) {
159+
int x = d[product[j]] * amount[j];
160+
ans += x - (dis * x) / 100.0;
161+
}
162+
return ans;
163+
}
164+
165+
private:
166+
int i = 0;
167+
int n;
168+
int discount;
169+
unordered_map<int, int> d;
170+
};
171+
172+
/**
173+
* Your Cashier object will be instantiated and called as such:
174+
* Cashier* obj = new Cashier(n, discount, products, prices);
175+
* double param_1 = obj->getBill(product,amount);
176+
*/
177+
```
83178

179+
### **Go**
180+
181+
```go
182+
type Cashier struct {
183+
i int
184+
n int
185+
discount int
186+
d map[int]int
187+
}
188+
189+
func Constructor(n int, discount int, products []int, prices []int) Cashier {
190+
d := map[int]int{}
191+
for i, product := range products {
192+
d[product] = prices[i]
193+
}
194+
return Cashier{0, n, discount, d}
195+
}
196+
197+
func (this *Cashier) GetBill(product []int, amount []int) (ans float64) {
198+
this.i++
199+
dis := 0
200+
if this.i%this.n == 0 {
201+
dis = this.discount
202+
}
203+
for j, p := range product {
204+
x := float64(this.d[p] * amount[j])
205+
ans += x - (float64(dis)*x)/100.0
206+
}
207+
return
208+
}
209+
210+
/**
211+
* Your Cashier object will be instantiated and called as such:
212+
* obj := Constructor(n, discount, products, prices);
213+
* param_1 := obj.GetBill(product,amount);
214+
*/
84215
```
85216

86217
### **...**

solution/1300-1399/1357.Apply Discount Every n Orders/README_EN.md

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,138 @@ cashier.getBill([2,3,5],[5,3,2]); // return 2500.0. 6<sup>th
7070
### **Python3**
7171

7272
```python
73-
73+
class Cashier:
74+
75+
def __init__(self, n: int, discount: int, products: List[int], prices: List[int]):
76+
self.i = 0
77+
self.n = n
78+
self.discount = discount
79+
self.d = {product: price for product, price in zip(products, prices)}
80+
81+
def getBill(self, product: List[int], amount: List[int]) -> float:
82+
self.i += 1
83+
discount = self.discount if self.i % self.n == 0 else 0
84+
ans = 0
85+
for p, a in zip(product, amount):
86+
x = self.d[p] * a
87+
ans += x - (discount * x) / 100
88+
return ans
89+
90+
91+
# Your Cashier object will be instantiated and called as such:
92+
# obj = Cashier(n, discount, products, prices)
93+
# param_1 = obj.getBill(product,amount)
7494
```
7595

7696
### **Java**
7797

7898
```java
99+
class Cashier {
100+
private int i;
101+
private int n;
102+
private int discount;
103+
private Map<Integer, Integer> d = new HashMap<>();
104+
105+
public Cashier(int n, int discount, int[] products, int[] prices) {
106+
this.n = n;
107+
this.discount = discount;
108+
for (int j = 0; j < products.length; ++j) {
109+
d.put(products[j], prices[j]);
110+
}
111+
}
112+
113+
public double getBill(int[] product, int[] amount) {
114+
int dis = (++i) % n == 0 ? discount : 0;
115+
double ans = 0;
116+
for (int j = 0; j < product.length; ++j) {
117+
int p = product[j], a = amount[j];
118+
int x = d.get(p) * a;
119+
ans += x - (dis * x) / 100.0;
120+
}
121+
return ans;
122+
}
123+
}
124+
125+
/**
126+
* Your Cashier object will be instantiated and called as such:
127+
* Cashier obj = new Cashier(n, discount, products, prices);
128+
* double param_1 = obj.getBill(product,amount);
129+
*/
130+
```
131+
132+
### **C++**
133+
134+
```cpp
135+
class Cashier {
136+
public:
137+
Cashier(int n, int discount, vector<int>& products, vector<int>& prices) {
138+
this->n = n;
139+
this->discount = discount;
140+
for (int j = 0; j < products.size(); ++j) {
141+
d[products[j]] = prices[j];
142+
}
143+
}
144+
145+
double getBill(vector<int> product, vector<int> amount) {
146+
int dis = (++i) % n == 0 ? discount : 0;
147+
double ans = 0;
148+
for (int j = 0; j < product.size(); ++j) {
149+
int x = d[product[j]] * amount[j];
150+
ans += x - (dis * x) / 100.0;
151+
}
152+
return ans;
153+
}
154+
155+
private:
156+
int i = 0;
157+
int n;
158+
int discount;
159+
unordered_map<int, int> d;
160+
};
161+
162+
/**
163+
* Your Cashier object will be instantiated and called as such:
164+
* Cashier* obj = new Cashier(n, discount, products, prices);
165+
* double param_1 = obj->getBill(product,amount);
166+
*/
167+
```
79168
169+
### **Go**
170+
171+
```go
172+
type Cashier struct {
173+
i int
174+
n int
175+
discount int
176+
d map[int]int
177+
}
178+
179+
func Constructor(n int, discount int, products []int, prices []int) Cashier {
180+
d := map[int]int{}
181+
for i, product := range products {
182+
d[product] = prices[i]
183+
}
184+
return Cashier{0, n, discount, d}
185+
}
186+
187+
func (this *Cashier) GetBill(product []int, amount []int) (ans float64) {
188+
this.i++
189+
dis := 0
190+
if this.i%this.n == 0 {
191+
dis = this.discount
192+
}
193+
for j, p := range product {
194+
x := float64(this.d[p] * amount[j])
195+
ans += x - (float64(dis)*x)/100.0
196+
}
197+
return
198+
}
199+
200+
/**
201+
* Your Cashier object will be instantiated and called as such:
202+
* obj := Constructor(n, discount, products, prices);
203+
* param_1 := obj.GetBill(product,amount);
204+
*/
80205
```
81206

82207
### **...**
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Cashier {
2+
public:
3+
Cashier(int n, int discount, vector<int>& products, vector<int>& prices) {
4+
this->n = n;
5+
this->discount = discount;
6+
for (int j = 0; j < products.size(); ++j) {
7+
d[products[j]] = prices[j];
8+
}
9+
}
10+
11+
double getBill(vector<int> product, vector<int> amount) {
12+
int dis = (++i) % n == 0 ? discount : 0;
13+
double ans = 0;
14+
for (int j = 0; j < product.size(); ++j) {
15+
int x = d[product[j]] * amount[j];
16+
ans += x - (dis * x) / 100.0;
17+
}
18+
return ans;
19+
}
20+
21+
private:
22+
int i = 0;
23+
int n;
24+
int discount;
25+
unordered_map<int, int> d;
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+
*/
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
type Cashier struct {
2+
i int
3+
n int
4+
discount int
5+
d map[int]int
6+
}
7+
8+
func Constructor(n int, discount int, products []int, prices []int) Cashier {
9+
d := map[int]int{}
10+
for i, product := range products {
11+
d[product] = prices[i]
12+
}
13+
return Cashier{0, n, discount, d}
14+
}
15+
16+
func (this *Cashier) GetBill(product []int, amount []int) (ans float64) {
17+
this.i++
18+
dis := 0
19+
if this.i%this.n == 0 {
20+
dis = this.discount
21+
}
22+
for j, p := range product {
23+
x := float64(this.d[p] * amount[j])
24+
ans += x - (float64(dis)*x)/100.0
25+
}
26+
return
27+
}
28+
29+
/**
30+
* Your Cashier object will be instantiated and called as such:
31+
* obj := Constructor(n, discount, products, prices);
32+
* param_1 := obj.GetBill(product,amount);
33+
*/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Cashier {
2+
private int i;
3+
private int n;
4+
private int discount;
5+
private Map<Integer, Integer> d = new HashMap<>();
6+
7+
public Cashier(int n, int discount, int[] products, int[] prices) {
8+
this.n = n;
9+
this.discount = discount;
10+
for (int j = 0; j < products.length; ++j) {
11+
d.put(products[j], prices[j]);
12+
}
13+
}
14+
15+
public double getBill(int[] product, int[] amount) {
16+
int dis = (++i) % n == 0 ? discount : 0;
17+
double ans = 0;
18+
for (int j = 0; j < product.length; ++j) {
19+
int p = product[j], a = amount[j];
20+
int x = d.get(p) * a;
21+
ans += x - (dis * x) / 100.0;
22+
}
23+
return ans;
24+
}
25+
}
26+
27+
/**
28+
* Your Cashier object will be instantiated and called as such:
29+
* Cashier obj = new Cashier(n, discount, products, prices);
30+
* double param_1 = obj.getBill(product,amount);
31+
*/

0 commit comments

Comments
 (0)