forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesign-an-atm-machine.cpp
More file actions
39 lines (34 loc) · 885 Bytes
/
design-an-atm-machine.cpp
File metadata and controls
39 lines (34 loc) · 885 Bytes
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
// Time: ctor: O(1)
// deposit: O(1)
// withdraw: O(1)
// Space: O(1)
// greedy
class ATM {
public:
ATM()
: vals_{20, 50, 100, 200, 500}
, cnt_(size(vals_)) {
}
void deposit(vector<int> banknotesCount) {
for (int i = 0; i < size(cnt_); ++i) {
cnt_[i] += banknotesCount[i];
}
}
vector<int> withdraw(int amount) {
vector<int> result(size(cnt_));
for (int i = size(vals_) - 1; i >= 0; --i) {
result[i] = min(static_cast<int64_t>(amount) / vals_[i], cnt_[i]);
amount -= result[i] * vals_[i];
}
if (amount) {
return {-1};
}
for (int i = 0; i < size(result); ++i) {
cnt_[i] -= result[i];
}
return result;
}
private:
const vector<int> vals_;
vector<int64_t> cnt_;
};