Skip to content

Commit bd9def9

Browse files
authored
feat: add solutions to lc problems: No.2240,2241 (#3335)
1 parent de21e8d commit bd9def9

File tree

5 files changed

+134
-3
lines changed

5 files changed

+134
-3
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ tags:
5959

6060
### 方法一:枚举
6161

62-
我们可以枚举购买钢笔的数量 $x$,对于每个 $x$,我们最多可以购买铅笔的数量为 $\frac{total - x \times cost1}{cost2}$,那么数量加 $1$ 即为 $x$ 的方案数。我们累加所有的 $x$ 的方案数,即为答案。
62+
我们可以枚举购买钢笔的数量 $x$,对于每个 $x$,我们最多可以购买铅笔的数量为 $\frac{\textit{total} - x \times \textit{cost1}}{\textit{cost2}}$,那么数量加 $1$ 即为 $x$ 的方案数。我们累加所有的 $x$ 的方案数,即为答案。
6363

6464
时间复杂度 $O(\frac{total}{cost1})$,空间复杂度 $O(1)$。
6565

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ The total number of ways to buy pens and pencils is 5 + 3 + 1 = 9.
5757

5858
<!-- solution:start -->
5959

60-
### Solution 1
60+
### Solution 1: Enumeration
61+
62+
We can enumerate the number of pens to buy, denoted as $x$. For each $x$, the maximum number of pencils we can buy is $\frac{\textit{total} - x \times \textit{cost1}}{\textit{cost2}}$. The number of ways for each $x$ is this value plus 1. We sum up the number of ways for all $x$ to get the answer.
63+
64+
The time complexity is $O(\frac{\textit{total}}{\textit{cost1}})$, and the space complexity is $O(1)$.
6165

6266
<!-- tabs:start -->
6367

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

+42
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,48 @@ func (this *ATM) Withdraw(amount int) []int {
239239
*/
240240
```
241241

242+
#### TypeScript
243+
244+
```ts
245+
class ATM {
246+
private cnt: number[];
247+
private d: number[];
248+
249+
constructor() {
250+
this.cnt = [0, 0, 0, 0, 0];
251+
this.d = [20, 50, 100, 200, 500];
252+
}
253+
254+
deposit(banknotesCount: number[]): void {
255+
for (let i = 0; i < banknotesCount.length; i++) {
256+
this.cnt[i] += banknotesCount[i];
257+
}
258+
}
259+
260+
withdraw(amount: number): number[] {
261+
let ans = [0, 0, 0, 0, 0];
262+
for (let i = 4; i >= 0; i--) {
263+
ans[i] = Math.min(Math.floor(amount / this.d[i]), this.cnt[i]);
264+
amount -= ans[i] * this.d[i];
265+
}
266+
if (amount > 0) {
267+
return [-1];
268+
}
269+
for (let i = 0; i < ans.length; i++) {
270+
this.cnt[i] -= ans[i];
271+
}
272+
return ans;
273+
}
274+
}
275+
276+
/**
277+
* Your ATM object will be instantiated and called as such:
278+
* var obj = new ATM()
279+
* obj.deposit(banknotesCount)
280+
* var param_2 = obj.withdraw(amount)
281+
*/
282+
```
283+
242284
<!-- tabs:end -->
243285

244286
<!-- solution:end -->

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

+49-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@ atm.withdraw(550); // Returns [0,1,0,0,1]. The machine uses 1 $50 banknot
8282

8383
<!-- solution:start -->
8484

85-
### Solution 1
85+
### Solution 1: Simulation
86+
87+
We use an array $d$ to record the denominations of the bills and an array $cnt$ to record the number of bills for each denomination.
88+
89+
For the `deposit` operation, we only need to add the number of bills for the corresponding denomination. The time complexity is $O(1)$.
90+
91+
For the `withdraw` operation, we enumerate the bills from largest to smallest denomination, taking out as many bills as possible without exceeding the $amount$. Then, we subtract the total value of the withdrawn bills from $amount$. If $amount$ is still greater than $0$ at the end, it means it's not possible to withdraw the $amount$ with the available bills, and we return $-1$. Otherwise, we return the number of bills withdrawn. The time complexity is $O(1)$.
8692

8793
<!-- tabs:start -->
8894

@@ -239,6 +245,48 @@ func (this *ATM) Withdraw(amount int) []int {
239245
*/
240246
```
241247

248+
#### TypeScript
249+
250+
```ts
251+
class ATM {
252+
private cnt: number[];
253+
private d: number[];
254+
255+
constructor() {
256+
this.cnt = [0, 0, 0, 0, 0];
257+
this.d = [20, 50, 100, 200, 500];
258+
}
259+
260+
deposit(banknotesCount: number[]): void {
261+
for (let i = 0; i < banknotesCount.length; i++) {
262+
this.cnt[i] += banknotesCount[i];
263+
}
264+
}
265+
266+
withdraw(amount: number): number[] {
267+
let ans = [0, 0, 0, 0, 0];
268+
for (let i = 4; i >= 0; i--) {
269+
ans[i] = Math.min(Math.floor(amount / this.d[i]), this.cnt[i]);
270+
amount -= ans[i] * this.d[i];
271+
}
272+
if (amount > 0) {
273+
return [-1];
274+
}
275+
for (let i = 0; i < ans.length; i++) {
276+
this.cnt[i] -= ans[i];
277+
}
278+
return ans;
279+
}
280+
}
281+
282+
/**
283+
* Your ATM object will be instantiated and called as such:
284+
* var obj = new ATM()
285+
* obj.deposit(banknotesCount)
286+
* var param_2 = obj.withdraw(amount)
287+
*/
288+
```
289+
242290
<!-- tabs:end -->
243291

244292
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class ATM {
2+
private cnt: number[];
3+
private d: number[];
4+
5+
constructor() {
6+
this.cnt = [0, 0, 0, 0, 0];
7+
this.d = [20, 50, 100, 200, 500];
8+
}
9+
10+
deposit(banknotesCount: number[]): void {
11+
for (let i = 0; i < banknotesCount.length; i++) {
12+
this.cnt[i] += banknotesCount[i];
13+
}
14+
}
15+
16+
withdraw(amount: number): number[] {
17+
let ans = [0, 0, 0, 0, 0];
18+
for (let i = 4; i >= 0; i--) {
19+
ans[i] = Math.min(Math.floor(amount / this.d[i]), this.cnt[i]);
20+
amount -= ans[i] * this.d[i];
21+
}
22+
if (amount > 0) {
23+
return [-1];
24+
}
25+
for (let i = 0; i < ans.length; i++) {
26+
this.cnt[i] -= ans[i];
27+
}
28+
return ans;
29+
}
30+
}
31+
32+
/**
33+
* Your ATM object will be instantiated and called as such:
34+
* var obj = new ATM()
35+
* obj.deposit(banknotesCount)
36+
* var param_2 = obj.withdraw(amount)
37+
*/

0 commit comments

Comments
 (0)