forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
35 lines (31 loc) · 848 Bytes
/
Solution.java
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
class ATM {
private long[] cnt = new long[5];
private int[] d = {20, 50, 100, 200, 500};
public ATM() {
}
public void deposit(int[] banknotesCount) {
for (int i = 0; i < banknotesCount.length; ++i) {
cnt[i] += banknotesCount[i];
}
}
public int[] withdraw(int amount) {
int[] ans = new int[5];
for (int i = 4; i >= 0; --i) {
ans[i] = (int) Math.min(amount / d[i], cnt[i]);
amount -= ans[i] * d[i];
}
if (amount > 0) {
return new int[] {-1};
}
for (int i = 0; i < 5; ++i) {
cnt[i] -= ans[i];
}
return ans;
}
}
/**
* Your ATM object will be instantiated and called as such:
* ATM obj = new ATM();
* obj.deposit(banknotesCount);
* int[] param_2 = obj.withdraw(amount);
*/