题目:
一个 ATM 机器,存有 5 种面值的钞票:20 ,50 ,100 ,200 和 500 美元。初始时,ATM 机是空的。用户可以用它存或者取任意数目的钱。
取款时,机器会优先取 较大 数额的钱。
比方说,你想取 50 的钞票,1 张 200 的钞票,那么机器会取出 200 的钞票。
但是,如果你想取 200 的钞票和1 张 500 的钞票,然后无法取出剩余的 500 钞票的存在,机器 不能 取 $200 的钞票。
请你实现 ATM 类:
ATM() 初始化 ATM 对象。
void deposit(int[] banknotesCount) 分别存入 50,200 和 20 ,100 ,500 钞票的数目,并且更新 ATM 机里取款后钞票的剩余数量。如果无法取出指定数额的钱,请返回 [-1] (这种情况下 不 取出任何钞票)。
示例 1:
输入:
["ATM", "deposit", "withdraw", "deposit", "withdraw", "withdraw"]
[[], [[0,0,1,2,1]], [600], [[0,1,0,1,1]], [600], [550]]
输出:
[null, null, [0,0,1,0,1], null, [-1], [0,1,0,0,1]]
解释:
ATM atm = new ATM();
atm.deposit([0,0,1,2,1]); // 存入 1 张 200 和 1 张 100 和 1 张 50 ,1 张 500 的钞票。
// 机器中剩余钞票数量为 [0,1,0,3,1] 。
atm.withdraw(600); // 返回 [-1] 。机器会尝试取出 100 ,所以取款请求会被拒绝。
// 由于请求被拒绝,机器中钞票的数量不会发生改变。
atm.withdraw(550); // 返回 [0,1,0,0,1] ,机器会返回 1 张 500 的钞票。
提示:
banknotesCount.length == 5
0 <= banknotesCount[i] <= 10^9
1 <= amount <= 10^9
总共 最多有 5000 次 withdraw 和 deposit 的调用。
函数 withdraw 和 deposit 至少各有 一次 调用。
java代码:
class ATM {
//int[] money = {20,50,100,200,500};
long[] quantitiy = new long[5];
public ATM() {
}
public void deposit(int[] banknotesCount) {
for (int i = 0; i < 5; i++) {
quantitiy[i] += banknotesCount[i];
}
}
public int[] withdraw(int amount) {
int[] ans = new int[5], demo = {-1};
int p = 4;
while (amount > 0) {
if (amount >= 500 && quantitiy[4] > 0) {
//需要和剩余
int need = amount / 500, extra = amount % 500;
if (need <= quantitiy[4]) { //取得出的话直接减去need,amount变为extra(剩余值)
ans[4] = need;
quantitiy[4] -= need;
amount = extra;
} else { //取不出的话变0
ans[4] = (int) quantitiy[4];
amount -= 500 * quantitiy[4];
quantitiy[4] = 0;
}
} else if (amount >= 200 && quantitiy[3] > 0) {
int need = amount / 200, extra = amount % 200;
if (need <= quantitiy[3]) {//取得出的话直接减去need,amount变为extra(剩余值)
ans[3] = need;
quantitiy[3] -= need;
amount = extra;
} else { //取不出的话变0
ans[3] = (int) quantitiy[3];
amount -= 200 * quantitiy[3];
quantitiy[3] = 0;
}
} else if (amount >= 100 && quantitiy[2] > 0) {
int need = amount / 100, extra = amount % 100;
if (need <= quantitiy[2]) {//取得出的话直接减去need,amount变为extra(剩余值)
ans[2] = need;
quantitiy[2] -= need;
amount = extra;
} else {//取不出的话变0
ans[2] = (int) quantitiy[2];
amount -= 100 * quantitiy[2];
quantitiy[2] = 0;
}
} else if (amount >= 50 && quantitiy[1] > 0) {
int need = amount / 50, extra = amount % 50;
if (need <= quantitiy[1]) {//取得出的话直接减去need,amount变为extra(剩余值)
ans[1] = need;
quantitiy[1] -= need;
amount = extra;
} else {//取不出的话变0
ans[1] = (int) quantitiy[1];
amount -= 50 * quantitiy[1];
quantitiy[1] = 0;
}
} else if (amount >= 20 && quantitiy[0] > 0) {
int need = amount / 20, extra = amount % 20;
if (need <= quantitiy[0]) {//取得出的话直接减去need,amount变为extra(剩余值)
ans[0] = need;
quantitiy[0] -= need;
amount = extra;
} else {//取不出的话变0
ans[0] = (int) quantitiy[0];
amount -= 20 * quantitiy[0];
quantitiy[0] = 0;
}
} else break;
}
//无法完全提取存款,需要把钱加回去
if (amount != 0) for (int i = 0; i < 5; i++) quantitiy[i] += ans[i];
//根据存款是否为0返回答案
return amount == 0 ? ans : demo;
}
}
/**
* Your ATM object will be instantiated and called as such:
* ATM obj = new ATM();
* obj.deposit(banknotesCount);
* int[] param_2 = obj.withdraw(amount);
*/
网友评论