|
1 |
| -export default function ATM() { |
2 |
| - const moneyStore = new Map([ |
3 |
| - [20, 0], |
4 |
| - [50, 0], |
5 |
| - [100, 0], |
6 |
| - [200, 0], |
7 |
| - [500, 0], |
8 |
| - ]); |
9 |
| - const indexToMoney = [20, 50, 100, 200, 500]; |
10 |
| - return { |
11 |
| - deposit(banknotesCount: number[]): void { |
12 |
| - for (const [index, count] of banknotesCount.entries()) { |
13 |
| - const key = indexToMoney[index]; |
14 |
| - if (!moneyStore.has(key)) { |
15 |
| - throw new Error("moneyStore Not Found"); |
16 |
| - } |
17 |
| - moneyStore.set(key, (moneyStore.get(key) ?? 0) + count); |
18 |
| - } |
19 |
| - }, |
20 |
| - |
21 |
| - withdraw(amount: number): number[] { |
22 |
| - const delta = Object.fromEntries([ |
23 |
| - [20, 0], |
24 |
| - [50, 0], |
25 |
| - [100, 0], |
26 |
| - [200, 0], |
27 |
| - [500, 0], |
28 |
| - ]); |
29 |
| - const changedStore = new Map(moneyStore); |
30 |
| - for (const money of [500, 200, 100, 50, 20]) { |
31 |
| - if ((changedStore.get(money) ?? 0) > 0 && amount >= money) { |
32 |
| - const d = Math.min( |
33 |
| - Math.floor(amount / money), |
34 |
| - changedStore.get(money) ?? 0 |
35 |
| - ); |
36 |
| - amount -= money * d; |
37 |
| - delta[money] += d; |
38 |
| - changedStore.set( |
39 |
| - money, |
40 |
| - -d + (changedStore.get(money) ?? 0) |
41 |
| - ); |
42 |
| - } |
43 |
| - } |
44 |
| - if (amount > 0) { |
45 |
| - return [-1]; |
46 |
| - } else { |
47 |
| - changedStore.forEach((value, key) => |
48 |
| - moneyStore.set(key, value) |
49 |
| - ); |
50 |
| - return Object.values(delta); |
51 |
| - } |
52 |
| - }, |
53 |
| - }; |
54 |
| -} |
| 1 | +export default function ATM() { |
| 2 | + const moneyStore = new Map([ |
| 3 | + [20, 0], |
| 4 | + [50, 0], |
| 5 | + [100, 0], |
| 6 | + [200, 0], |
| 7 | + [500, 0], |
| 8 | + ]); |
| 9 | + const indexToMoney = [20, 50, 100, 200, 500]; |
| 10 | + return { |
| 11 | + deposit(banknotesCount: number[]): void { |
| 12 | + for (const [index, count] of banknotesCount.entries()) { |
| 13 | + const key = indexToMoney[index]; |
| 14 | + if (!moneyStore.has(key)) { |
| 15 | + throw new Error("moneyStore Not Found"); |
| 16 | + } |
| 17 | + moneyStore.set(key, (moneyStore.get(key) ?? 0) + count); |
| 18 | + } |
| 19 | + }, |
| 20 | + |
| 21 | + withdraw(amount: number): number[] { |
| 22 | + const delta = Object.fromEntries([ |
| 23 | + [20, 0], |
| 24 | + [50, 0], |
| 25 | + [100, 0], |
| 26 | + [200, 0], |
| 27 | + [500, 0], |
| 28 | + ]); |
| 29 | + const changedStore = new Map(moneyStore); |
| 30 | + for (const money of [500, 200, 100, 50, 20]) { |
| 31 | + if ((changedStore.get(money) ?? 0) > 0 && amount >= money) { |
| 32 | + const d = Math.min( |
| 33 | + Math.floor(amount / money), |
| 34 | + changedStore.get(money) ?? 0, |
| 35 | + ); |
| 36 | + amount -= money * d; |
| 37 | + delta[money] += d; |
| 38 | + changedStore.set( |
| 39 | + money, |
| 40 | + -d + (changedStore.get(money) ?? 0), |
| 41 | + ); |
| 42 | + } |
| 43 | + } |
| 44 | + if (amount > 0) { |
| 45 | + return [-1]; |
| 46 | + } else { |
| 47 | + changedStore.forEach((value, key) => |
| 48 | + moneyStore.set(key, value) |
| 49 | + ); |
| 50 | + return Object.values(delta); |
| 51 | + } |
| 52 | + }, |
| 53 | + }; |
| 54 | +} |
0 commit comments