Skip to content

feat: add solutions to lc problems: No.2240,2241 #3335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ tags:

### 方法一:枚举

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

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

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

<!-- solution:start -->

### Solution 1
### Solution 1: Enumeration

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.

The time complexity is $O(\frac{\textit{total}}{\textit{cost1}})$, and the space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down
42 changes: 42 additions & 0 deletions solution/2200-2299/2241.Design an ATM Machine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,48 @@ func (this *ATM) Withdraw(amount int) []int {
*/
```

#### TypeScript

```ts
class ATM {
private cnt: number[];
private d: number[];

constructor() {
this.cnt = [0, 0, 0, 0, 0];
this.d = [20, 50, 100, 200, 500];
}

deposit(banknotesCount: number[]): void {
for (let i = 0; i < banknotesCount.length; i++) {
this.cnt[i] += banknotesCount[i];
}
}

withdraw(amount: number): number[] {
let ans = [0, 0, 0, 0, 0];
for (let i = 4; i >= 0; i--) {
ans[i] = Math.min(Math.floor(amount / this.d[i]), this.cnt[i]);
amount -= ans[i] * this.d[i];
}
if (amount > 0) {
return [-1];
}
for (let i = 0; i < ans.length; i++) {
this.cnt[i] -= ans[i];
}
return ans;
}
}

/**
* Your ATM object will be instantiated and called as such:
* var obj = new ATM()
* obj.deposit(banknotesCount)
* var param_2 = obj.withdraw(amount)
*/
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
50 changes: 49 additions & 1 deletion solution/2200-2299/2241.Design an ATM Machine/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ atm.withdraw(550); // Returns [0,1,0,0,1]. The machine uses 1 $50 banknot

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

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.

For the `deposit` operation, we only need to add the number of bills for the corresponding denomination. The time complexity is $O(1)$.

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)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -239,6 +245,48 @@ func (this *ATM) Withdraw(amount int) []int {
*/
```

#### TypeScript

```ts
class ATM {
private cnt: number[];
private d: number[];

constructor() {
this.cnt = [0, 0, 0, 0, 0];
this.d = [20, 50, 100, 200, 500];
}

deposit(banknotesCount: number[]): void {
for (let i = 0; i < banknotesCount.length; i++) {
this.cnt[i] += banknotesCount[i];
}
}

withdraw(amount: number): number[] {
let ans = [0, 0, 0, 0, 0];
for (let i = 4; i >= 0; i--) {
ans[i] = Math.min(Math.floor(amount / this.d[i]), this.cnt[i]);
amount -= ans[i] * this.d[i];
}
if (amount > 0) {
return [-1];
}
for (let i = 0; i < ans.length; i++) {
this.cnt[i] -= ans[i];
}
return ans;
}
}

/**
* Your ATM object will be instantiated and called as such:
* var obj = new ATM()
* obj.deposit(banknotesCount)
* var param_2 = obj.withdraw(amount)
*/
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
37 changes: 37 additions & 0 deletions solution/2200-2299/2241.Design an ATM Machine/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class ATM {
private cnt: number[];
private d: number[];

constructor() {
this.cnt = [0, 0, 0, 0, 0];
this.d = [20, 50, 100, 200, 500];
}

deposit(banknotesCount: number[]): void {
for (let i = 0; i < banknotesCount.length; i++) {
this.cnt[i] += banknotesCount[i];
}
}

withdraw(amount: number): number[] {
let ans = [0, 0, 0, 0, 0];
for (let i = 4; i >= 0; i--) {
ans[i] = Math.min(Math.floor(amount / this.d[i]), this.cnt[i]);
amount -= ans[i] * this.d[i];
}
if (amount > 0) {
return [-1];
}
for (let i = 0; i < ans.length; i++) {
this.cnt[i] -= ans[i];
}
return ans;
}
}

/**
* Your ATM object will be instantiated and called as such:
* var obj = new ATM()
* obj.deposit(banknotesCount)
* var param_2 = obj.withdraw(amount)
*/
Loading