Skip to content

Commit 5c69b7c

Browse files
committed
feat: add solutions to lc problems: No.2240,2241
* No.2240.Number of Ways to Buy Pens and Pencils * No.2241.Design an ATM Machine
1 parent 8d588da commit 5c69b7c

File tree

13 files changed

+467
-53
lines changed

13 files changed

+467
-53
lines changed

solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README.md

+28-12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242

4343
<!-- 这里可写通用的实现逻辑 -->
4444

45+
**方法一:枚举**
46+
47+
我们可以枚举购买钢笔的数量 $x$,那么对于每个 $x$,我们最多可以购买铅笔的数量为 $\frac{total - x \times cost1}{cost2}$,那么方案数为 $y + 1$。我们累加所有的 $x$ 的方案数,即为答案。
48+
49+
时间复杂度 $O(\frac{total}{cost1})$,空间复杂度 $O(1)$。
50+
4551
<!-- tabs:start -->
4652

4753
### **Python3**
@@ -53,8 +59,8 @@ class Solution:
5359
def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int:
5460
ans = 0
5561
for x in range(total // cost1 + 1):
56-
v = total - x * cost1
57-
ans += v // cost2 + 1
62+
y = (total - (x * cost1)) // cost2 + 1
63+
ans += y
5864
return ans
5965
```
6066

@@ -67,8 +73,8 @@ class Solution {
6773
public long waysToBuyPensPencils(int total, int cost1, int cost2) {
6874
long ans = 0;
6975
for (int x = 0; x <= total / cost1; ++x) {
70-
int v = total - x * cost1;
71-
ans += v / cost2 + 1;
76+
int y = (total - x * cost1) / cost2 + 1;
77+
ans += y;
7278
}
7379
return ans;
7480
}
@@ -83,8 +89,8 @@ public:
8389
long long waysToBuyPensPencils(int total, int cost1, int cost2) {
8490
long long ans = 0;
8591
for (int x = 0; x <= total / cost1; ++x) {
86-
int v = total - x * cost1;
87-
ans += v / cost2 + 1;
92+
int y = (total - x * cost1) / cost2 + 1;
93+
ans += y;
8894
}
8995
return ans;
9096
}
@@ -94,20 +100,30 @@ public:
94100
### **Go**
95101
96102
```go
97-
func waysToBuyPensPencils(total int, cost1 int, cost2 int) int64 {
98-
var ans int64
103+
func waysToBuyPensPencils(total int, cost1 int, cost2 int) (ans int64) {
99104
for x := 0; x <= total/cost1; x++ {
100-
v := total - x*cost1
101-
ans += int64(v/cost2 + 1)
105+
y := (total-x*cost1)/cost2 + 1
106+
ans += int64(y)
102107
}
103-
return ans
108+
return
104109
}
105110
```
106111

107112
### **TypeScript**
108113

109114
```ts
110-
115+
function waysToBuyPensPencils(
116+
total: number,
117+
cost1: number,
118+
cost2: number,
119+
): number {
120+
let ans = 0;
121+
for (let x = 0; x <= Math.floor(total / cost1); ++x) {
122+
const y = Math.floor((total - x * cost1) / cost2) + 1;
123+
ans += y;
124+
}
125+
return ans;
126+
}
111127
```
112128

113129
### **...**

solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README_EN.md

+22-12
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ class Solution:
4747
def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int:
4848
ans = 0
4949
for x in range(total // cost1 + 1):
50-
v = total - x * cost1
51-
ans += v // cost2 + 1
50+
y = (total - (x * cost1)) // cost2 + 1
51+
ans += y
5252
return ans
5353
```
5454

@@ -59,8 +59,8 @@ class Solution {
5959
public long waysToBuyPensPencils(int total, int cost1, int cost2) {
6060
long ans = 0;
6161
for (int x = 0; x <= total / cost1; ++x) {
62-
int v = total - x * cost1;
63-
ans += v / cost2 + 1;
62+
int y = (total - x * cost1) / cost2 + 1;
63+
ans += y;
6464
}
6565
return ans;
6666
}
@@ -75,8 +75,8 @@ public:
7575
long long waysToBuyPensPencils(int total, int cost1, int cost2) {
7676
long long ans = 0;
7777
for (int x = 0; x <= total / cost1; ++x) {
78-
int v = total - x * cost1;
79-
ans += v / cost2 + 1;
78+
int y = (total - x * cost1) / cost2 + 1;
79+
ans += y;
8080
}
8181
return ans;
8282
}
@@ -86,20 +86,30 @@ public:
8686
### **Go**
8787
8888
```go
89-
func waysToBuyPensPencils(total int, cost1 int, cost2 int) int64 {
90-
var ans int64
89+
func waysToBuyPensPencils(total int, cost1 int, cost2 int) (ans int64) {
9190
for x := 0; x <= total/cost1; x++ {
92-
v := total - x*cost1
93-
ans += int64(v/cost2 + 1)
91+
y := (total-x*cost1)/cost2 + 1
92+
ans += int64(y)
9493
}
95-
return ans
94+
return
9695
}
9796
```
9897

9998
### **TypeScript**
10099

101100
```ts
102-
101+
function waysToBuyPensPencils(
102+
total: number,
103+
cost1: number,
104+
cost2: number,
105+
): number {
106+
let ans = 0;
107+
for (let x = 0; x <= Math.floor(total / cost1); ++x) {
108+
const y = Math.floor((total - x * cost1) / cost2) + 1;
109+
ans += y;
110+
}
111+
return ans;
112+
}
103113
```
104114

105115
### **...**

solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ class Solution {
33
long long waysToBuyPensPencils(int total, int cost1, int cost2) {
44
long long ans = 0;
55
for (int x = 0; x <= total / cost1; ++x) {
6-
int v = total - x * cost1;
7-
ans += v / cost2 + 1;
6+
int y = (total - x * cost1) / cost2 + 1;
7+
ans += y;
88
}
99
return ans;
1010
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
func waysToBuyPensPencils(total int, cost1 int, cost2 int) int64 {
2-
var ans int64
1+
func waysToBuyPensPencils(total int, cost1 int, cost2 int) (ans int64) {
32
for x := 0; x <= total/cost1; x++ {
4-
v := total - x*cost1
5-
ans += int64(v/cost2 + 1)
3+
y := (total-x*cost1)/cost2 + 1
4+
ans += int64(y)
65
}
7-
return ans
6+
return
87
}

solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ class Solution {
22
public long waysToBuyPensPencils(int total, int cost1, int cost2) {
33
long ans = 0;
44
for (int x = 0; x <= total / cost1; ++x) {
5-
int v = total - x * cost1;
6-
ans += v / cost2 + 1;
5+
int y = (total - x * cost1) / cost2 + 1;
6+
ans += y;
77
}
88
return ans;
99
}

solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ class Solution:
22
def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int:
33
ans = 0
44
for x in range(total // cost1 + 1):
5-
v = total - x * cost1
6-
ans += v // cost2 + 1
5+
y = (total - (x * cost1)) // cost2 + 1
6+
ans += y
77
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function waysToBuyPensPencils(
2+
total: number,
3+
cost1: number,
4+
cost2: number,
5+
): number {
6+
let ans = 0;
7+
for (let x = 0; x <= Math.floor(total / cost1); ++x) {
8+
const y = Math.floor((total - x * cost1) / cost2) + 1;
9+
ans += y;
10+
}
11+
return ans;
12+
}

solution/2200-2299/2241.Design an ATM Machine/README.md

+140-6
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ atm.withdraw(550); // 返回 [0,1,0,0,1] ,机器会返回 1 张 $50 的
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
**方法一:模拟**
64+
65+
我们用一个数组 $d$ 记录钞票面额,用一个数组 $cnt$ 记录每种面额的钞票数量。
66+
67+
对于 `deposit` 操作,我们只需要将对应面额的钞票数量加上即可。时间复杂度 $O(1)$。
68+
69+
对于 `withdraw` 操作,我们从大到小枚举每种面额的钞票,取出尽可能多且不超过 $amount$ 的钞票,然后将 $amount$ 减去取出的钞票面额之和,如果最后 $amount$ 仍大于 $0$,说明无法取出 $amount$ 的钞票,返回 $-1$ 即可。否则,返回取出的钞票数量即可。时间复杂度 $O(1)$。
70+
6371
<!-- tabs:start -->
6472

6573
### **Python3**
@@ -70,22 +78,22 @@ atm.withdraw(550); // 返回 [0,1,0,0,1] ,机器会返回 1 张 $50 的
7078
class ATM:
7179
def __init__(self):
7280
self.cnt = [0] * 5
73-
self.m = [500, 200, 100, 50, 20]
81+
self.d = [20, 50, 100, 200, 500]
7482

7583
def deposit(self, banknotesCount: List[int]) -> None:
76-
for i, v in enumerate(banknotesCount[::-1]):
84+
for i, v in enumerate(banknotesCount):
7785
self.cnt[i] += v
7886

7987
def withdraw(self, amount: int) -> List[int]:
8088
ans = [0] * 5
81-
for i, n in enumerate(self.cnt):
82-
ans[i] = min(amount // self.m[i], n)
83-
amount -= self.m[i] * ans[i]
89+
for i in range(4, -1, -1):
90+
ans[i] = min(amount // self.d[i], self.cnt[i])
91+
amount -= ans[i] * self.d[i]
8492
if amount > 0:
8593
return [-1]
8694
for i, v in enumerate(ans):
8795
self.cnt[i] -= v
88-
return ans[::-1]
96+
return ans
8997

9098

9199
# Your ATM object will be instantiated and called as such:
@@ -99,7 +107,133 @@ class ATM:
99107
<!-- 这里可写当前语言的特殊实现逻辑 -->
100108

101109
```java
110+
class ATM {
111+
private long[] cnt = new long[5];
112+
private int[] d = {20, 50, 100, 200, 500};
113+
114+
public ATM() {
115+
116+
}
117+
118+
public void deposit(int[] banknotesCount) {
119+
for (int i = 0; i < banknotesCount.length; ++i) {
120+
cnt[i] += banknotesCount[i];
121+
}
122+
}
123+
124+
public int[] withdraw(int amount) {
125+
int[] ans = new int[5];
126+
for (int i = 4; i >= 0; --i) {
127+
ans[i] = (int) Math.min(amount / d[i], cnt[i]);
128+
amount -= ans[i] * d[i];
129+
}
130+
if (amount > 0) {
131+
return new int[] {-1};
132+
}
133+
for (int i = 0; i < 5; ++i) {
134+
cnt[i] -= ans[i];
135+
}
136+
return ans;
137+
}
138+
}
139+
140+
/**
141+
* Your ATM object will be instantiated and called as such:
142+
* ATM obj = new ATM();
143+
* obj.deposit(banknotesCount);
144+
* int[] param_2 = obj.withdraw(amount);
145+
*/
146+
```
147+
148+
### **C++**
149+
150+
```cpp
151+
class ATM {
152+
public:
153+
ATM() {
154+
155+
}
156+
157+
void deposit(vector<int> banknotesCount) {
158+
for (int i = 0; i < banknotesCount.size(); ++i) {
159+
cnt[i] += banknotesCount[i];
160+
}
161+
}
162+
163+
vector<int> withdraw(int amount) {
164+
vector<int> ans(5);
165+
for (int i = 4; ~i; --i) {
166+
ans[i] = min(1ll * amount / d[i], cnt[i]);
167+
amount -= ans[i] * d[i];
168+
}
169+
if (amount > 0) {
170+
return {-1};
171+
}
172+
for (int i = 0; i < 5; ++i) {
173+
cnt[i] -= ans[i];
174+
}
175+
return ans;
176+
}
177+
178+
private:
179+
long long cnt[5] = {0};
180+
int d[5] = {20, 50, 100, 200, 500};
181+
};
182+
183+
/**
184+
* Your ATM object will be instantiated and called as such:
185+
* ATM* obj = new ATM();
186+
* obj->deposit(banknotesCount);
187+
* vector<int> param_2 = obj->withdraw(amount);
188+
*/
189+
```
102190
191+
### **Go**
192+
193+
```go
194+
type ATM struct {
195+
d [5]int
196+
cnt [5]int
197+
}
198+
199+
func Constructor() ATM {
200+
return ATM{[5]int{20, 50, 100, 200, 500}, [5]int{}}
201+
}
202+
203+
func (this *ATM) Deposit(banknotesCount []int) {
204+
for i, v := range banknotesCount {
205+
this.cnt[i] += v
206+
}
207+
}
208+
209+
func (this *ATM) Withdraw(amount int) []int {
210+
ans := make([]int, 5)
211+
for i := 4; i >= 0; i-- {
212+
ans[i] = min(amount/this.d[i], this.cnt[i])
213+
amount -= ans[i] * this.d[i]
214+
}
215+
if amount > 0 {
216+
return []int{-1}
217+
}
218+
for i, v := range ans {
219+
this.cnt[i] -= v
220+
}
221+
return ans
222+
}
223+
224+
func min(a, b int) int {
225+
if a < b {
226+
return a
227+
}
228+
return b
229+
}
230+
231+
/**
232+
* Your ATM object will be instantiated and called as such:
233+
* obj := Constructor();
234+
* obj.Deposit(banknotesCount);
235+
* param_2 := obj.Withdraw(amount);
236+
*/
103237
```
104238

105239
### **TypeScript**

0 commit comments

Comments
 (0)