diff --git a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README.md b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README.md index dcdf52b7e82fd..9666fb60ed647 100644 --- a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README.md +++ b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README.md @@ -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)$。 diff --git a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README_EN.md b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README_EN.md index 7e6f4037e915b..d98e1e4472a58 100644 --- a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README_EN.md +++ b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README_EN.md @@ -57,7 +57,11 @@ The total number of ways to buy pens and pencils is 5 + 3 + 1 = 9. -### 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)$. diff --git a/solution/2200-2299/2241.Design an ATM Machine/README.md b/solution/2200-2299/2241.Design an ATM Machine/README.md index 503ca9a2b6c92..82009c6b5aa78 100644 --- a/solution/2200-2299/2241.Design an ATM Machine/README.md +++ b/solution/2200-2299/2241.Design an ATM Machine/README.md @@ -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) + */ +``` + diff --git a/solution/2200-2299/2241.Design an ATM Machine/README_EN.md b/solution/2200-2299/2241.Design an ATM Machine/README_EN.md index a35605b8ec5d7..3c24e07cb1e50 100644 --- a/solution/2200-2299/2241.Design an ATM Machine/README_EN.md +++ b/solution/2200-2299/2241.Design an ATM Machine/README_EN.md @@ -82,7 +82,13 @@ atm.withdraw(550); // Returns [0,1,0,0,1]. The machine uses 1 $50 banknot -### 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)$. @@ -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) + */ +``` + diff --git a/solution/2200-2299/2241.Design an ATM Machine/Solution.ts b/solution/2200-2299/2241.Design an ATM Machine/Solution.ts new file mode 100644 index 0000000000000..e51670a0d37f6 --- /dev/null +++ b/solution/2200-2299/2241.Design an ATM Machine/Solution.ts @@ -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) + */