|
1 | 1 | # [2241.Design an ATM Machine][title] |
2 | 2 |
|
3 | | -> [!WARNING|style:flat] |
4 | | -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) |
5 | | -
|
6 | 3 | ## Description |
| 4 | +There is an ATM machine that stores banknotes of `5` denominations: `20`, `50`, `100`, `200`, and `500` dollars. Initially the ATM is empty. The user can use the machine to deposit or withdraw any amount of money. |
7 | 5 |
|
8 | | -**Example 1:** |
| 6 | +When withdrawing, the machine prioritizes using banknotes of **larger** values. |
9 | 7 |
|
10 | | -``` |
11 | | -Input: a = "11", b = "1" |
12 | | -Output: "100" |
13 | | -``` |
| 8 | +- For example, if you want to withdraw `$300` and there are `2` `$50` banknotes, `1` `$100` banknote, and `1` `$200` banknote, then the machine will use the `$100` and `$200` banknotes. |
| 9 | +- However, if you try to withdraw `$600` and there are `3` `$200` banknotes and `1` `$500` banknote, then the withdraw request will be rejected because the machine will first try to use the `$500` banknote and then be unable to use banknotes to complete the remaining `$100`. Note that the machine is **not** allowed to use the `$200` banknotes instead of the `$500` banknote. |
14 | 10 |
|
15 | | -## 题意 |
16 | | -> ... |
| 11 | +Implement the ATM class: |
17 | 12 |
|
18 | | -## 题解 |
| 13 | +- `ATM()` Initializes the ATM object. |
| 14 | +- `void deposit(int[] banknotesCount)` Deposits new banknotes in the order `$20`, `$50`, `$100`, `$200`, and `$500`. |
| 15 | +- `int[] withdraw(int amount)` Returns an array of length `5` of the number of banknotes that will be handed to the user in the order `$20`, `$50`, `$100`, `$200`, and `$500`, and update the number of banknotes in the ATM after withdrawing. Returns `[-1]` if it is not possible (do **not** withdraw any banknotes in this case). |
19 | 16 |
|
20 | | -### 思路1 |
21 | | -> ... |
22 | | -Design an ATM Machine |
23 | | -```go |
24 | | -``` |
| 17 | +**Example 1:** |
25 | 18 |
|
| 19 | +``` |
| 20 | +Input |
| 21 | +["ATM", "deposit", "withdraw", "deposit", "withdraw", "withdraw"] |
| 22 | +[[], [[0,0,1,2,1]], [600], [[0,1,0,1,1]], [600], [550]] |
| 23 | +Output |
| 24 | +[null, null, [0,0,1,0,1], null, [-1], [0,1,0,0,1]] |
| 25 | +
|
| 26 | +Explanation |
| 27 | +ATM atm = new ATM(); |
| 28 | +atm.deposit([0,0,1,2,1]); // Deposits 1 $100 banknote, 2 $200 banknotes, |
| 29 | + // and 1 $500 banknote. |
| 30 | +atm.withdraw(600); // Returns [0,0,1,0,1]. The machine uses 1 $100 banknote |
| 31 | + // and 1 $500 banknote. The banknotes left over in the |
| 32 | + // machine are [0,0,0,2,0]. |
| 33 | +atm.deposit([0,1,0,1,1]); // Deposits 1 $50, $200, and $500 banknote. |
| 34 | + // The banknotes in the machine are now [0,1,0,3,1]. |
| 35 | +atm.withdraw(600); // Returns [-1]. The machine will try to use a $500 banknote |
| 36 | + // and then be unable to complete the remaining $100, |
| 37 | + // so the withdraw request will be rejected. |
| 38 | + // Since the request is rejected, the number of banknotes |
| 39 | + // in the machine is not modified. |
| 40 | +atm.withdraw(550); // Returns [0,1,0,0,1]. The machine uses 1 $50 banknote |
| 41 | + // and 1 $500 banknote. |
| 42 | +``` |
26 | 43 |
|
27 | 44 | ## 结语 |
28 | 45 |
|
|
0 commit comments