diff --git a/leetcode/2201-2300/2241.Design-an-ATM-Machine/README.md b/leetcode/2201-2300/2241.Design-an-ATM-Machine/README.md index d7bcfe292..cb794a8dd 100755 --- a/leetcode/2201-2300/2241.Design-an-ATM-Machine/README.md +++ b/leetcode/2201-2300/2241.Design-an-ATM-Machine/README.md @@ -1,28 +1,45 @@ # [2241.Design an ATM Machine][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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. -**Example 1:** +When withdrawing, the machine prioritizes using banknotes of **larger** values. -``` -Input: a = "11", b = "1" -Output: "100" -``` +- 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. +- 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. -## 题意 -> ... +Implement the ATM class: -## 题解 +- `ATM()` Initializes the ATM object. +- `void deposit(int[] banknotesCount)` Deposits new banknotes in the order `$20`, `$50`, `$100`, `$200`, and `$500`. +- `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). -### 思路1 -> ... -Design an ATM Machine -```go -``` +**Example 1:** +``` +Input +["ATM", "deposit", "withdraw", "deposit", "withdraw", "withdraw"] +[[], [[0,0,1,2,1]], [600], [[0,1,0,1,1]], [600], [550]] +Output +[null, null, [0,0,1,0,1], null, [-1], [0,1,0,0,1]] + +Explanation +ATM atm = new ATM(); +atm.deposit([0,0,1,2,1]); // Deposits 1 $100 banknote, 2 $200 banknotes, + // and 1 $500 banknote. +atm.withdraw(600); // Returns [0,0,1,0,1]. The machine uses 1 $100 banknote + // and 1 $500 banknote. The banknotes left over in the + // machine are [0,0,0,2,0]. +atm.deposit([0,1,0,1,1]); // Deposits 1 $50, $200, and $500 banknote. + // The banknotes in the machine are now [0,1,0,3,1]. +atm.withdraw(600); // Returns [-1]. The machine will try to use a $500 banknote + // and then be unable to complete the remaining $100, + // so the withdraw request will be rejected. + // Since the request is rejected, the number of banknotes + // in the machine is not modified. +atm.withdraw(550); // Returns [0,1,0,0,1]. The machine uses 1 $50 banknote + // and 1 $500 banknote. +``` ## 结语 diff --git a/leetcode/2201-2300/2241.Design-an-ATM-Machine/Solution.go b/leetcode/2201-2300/2241.Design-an-ATM-Machine/Solution.go index d115ccf5e..1eb113eb9 100644 --- a/leetcode/2201-2300/2241.Design-an-ATM-Machine/Solution.go +++ b/leetcode/2201-2300/2241.Design-an-ATM-Machine/Solution.go @@ -1,5 +1,67 @@ package Solution -func Solution(x bool) bool { - return x +type ATM struct { + value [5]int + atm [5]int +} + +func Constructor() ATM { + return ATM{ + value: [5]int{20, 50, 100, 200, 500}, + atm: [5]int{}, + } +} + +func (this *ATM) Deposit(banknotesCount []int) { + for i := range 5 { + this.atm[i] += banknotesCount[i] + } +} + +func (this *ATM) Withdraw(amount int) []int { + index := 4 + ret := make([]int, 5) + for ; index >= 0; index-- { + if this.atm[index] != 0 { + break + } + } + if index == -1 { + return []int{-1} + } + source := this.atm + for ; index >= 0 && amount > 0; index-- { + if this.atm[index] > 0 { + c := amount / this.value[index] + use := min(c, this.atm[index]) + amount -= use * this.value[index] + this.atm[index] = max(0, this.atm[index]-use) + ret[index] = use + } + } + if amount > 0 { + // 不要见 + this.atm = source + return []int{-1} + } + return ret +} + +type op struct { + name byte + banknotesCount []int + amount int +} + +func Solution(opts []op) [][]int { + var ret [][]int + atm := Constructor() + for _, o := range opts { + if o.name == 'd' { + atm.Deposit(o.banknotesCount) + continue + } + ret = append(ret, atm.Withdraw(o.amount)) + } + return ret } diff --git a/leetcode/2201-2300/2241.Design-an-ATM-Machine/Solution_test.go b/leetcode/2201-2300/2241.Design-an-ATM-Machine/Solution_test.go index 14ff50eb4..5be79041b 100644 --- a/leetcode/2201-2300/2241.Design-an-ATM-Machine/Solution_test.go +++ b/leetcode/2201-2300/2241.Design-an-ATM-Machine/Solution_test.go @@ -10,12 +10,16 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []op + expect [][]int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []op{ + {'d', []int{0, 0, 1, 2, 1}, 0}, + {'w', []int{}, 600}, + {'d', []int{0, 1, 0, 1, 1}, 0}, + {'w', []int{}, 600}, + {'w', []int{}, 550}, + }, [][]int{{0, 0, 1, 0, 1}, {-1}, {0, 1, 0, 0, 1}}}, } // 开始测试 @@ -30,10 +34,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }