-
Notifications
You must be signed in to change notification settings - Fork 0
/
2241. Design an ATM Machine.js
141 lines (116 loc) · 4.87 KB
/
2241. Design an ATM Machine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
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.
When withdrawing, the machine prioritizes using banknotes of larger values.
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).
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.
Constraints:
banknotesCount.length == 5
0 <= banknotesCount[i] <= 109
1 <= amount <= 109
At most 5000 calls in total will be made to withdraw and deposit.
At least one call will be made to each function withdraw and deposit.
*/
var ATM = function() {
// . . . . . . . . . .20 | 50 | 100 | 200 | 500
this.denominations = [ 0, 0, 0, 0, 0];
// . . . . . . . ..at: 0. 1. 2. 3. 4.
};
/**
* @param {number[]} banknotesCount
* @return {void}
*/
ATM.prototype.deposit = function(banknotesCount) {
for(let k in banknotesCount) {
this.denominations[k] += banknotesCount[k];
}
};
/**
* @param {number} amount
* @return {number[]}
*/
ATM.prototype.withdraw = function(amount) {
let rest = amount;
let reqNotes = 0;
let takeNotesCount = 0;
const banknotes = [0, 0, 0, 0, 0];
const [
available20,
available50,
available100,
available200,
available500
] = this.denominations;
// 500
reqNotes = Math.floor(rest / 500);
if (reqNotes > 0 && available500 > 0) {
takeNotesCount = Math.min(available500, reqNotes);
banknotes[4] = takeNotesCount;
rest -= takeNotesCount * 500;
}
// 200
reqNotes = Math.floor(rest / 200);
if (reqNotes > 0 && available200 > 0) {
takeNotesCount = Math.min(available200, reqNotes);
banknotes[3] = takeNotesCount;
rest -= takeNotesCount * 200;
}
// 100
reqNotes = Math.floor(rest / 100);
if (reqNotes > 0 && available100 > 0) {
takeNotesCount = Math.min(available100, reqNotes);
banknotes[2] = takeNotesCount;
rest -= takeNotesCount * 100;
}
// 50
reqNotes = Math.floor(rest / 50);
if (reqNotes > 0 && available50 > 0) {
takeNotesCount = Math.min(available50, reqNotes);
banknotes[1] = takeNotesCount;
rest -= takeNotesCount * 50;
}
// 20
reqNotes = Math.floor(rest / 20);
if (reqNotes > 0 && available20 > 0) {
takeNotesCount = Math.min(available20, reqNotes);
banknotes[0] = takeNotesCount;
rest -= takeNotesCount * 20;
}
const succeed = !(amount > 0 && rest === amount || rest > 0);
if (succeed) {
this.deposit(banknotes.map(v => 0 - v));
return banknotes;
}
return [-1];
};
/**
* Your ATM object will be instantiated and called as such:
* var obj = new ATM()
* obj.deposit(banknotesCount)
* var param_2 = obj.withdraw(amount)
*/