系列文章目录
【拒绝算法PUA】0x00-位运算
【拒绝算法PUA】0x01- 区间比较技巧
【拒绝算法PUA】0x02- 区间合并技巧
【拒绝算法PUA】0x03 - LeetCode 排序类型刷题
【拒绝算法PUA】LeetCode每日一题系列刷题汇总-2025年持续刷新中
C++刷题技巧总结:
[温习C/C++]0x04 刷题基础编码技巧
LeetCode 2241. 设计一个 ATM 机器
链接
题目
一个 ATM 机器,存有 5 种面值的钞票:20 ,50 ,100 ,200 和 500 美元。初始时,ATM 机是空的。用户可以用它存或者取任意数目的钱。
取款时,机器会优先取 较大 数额的钱。
比方说,你想取 $300 ,并且机器里有 2 张 $50 的钞票,1 张 $100 的钞票和1 张 $200 的钞票,那么机器会取出 $100 和 $200 的钞票。
但是,如果你想取 $600 ,机器里有 3 张 $200 的钞票和1 张 $500 的钞票,那么取款请求会被拒绝,因为机器会先取出 $500 的钞票,然后无法取出剩余的 $100 。注意,因为有 $500 钞票的存在,机器 不能 取 $200 的钞票。
请你实现 ATM 类:
ATM() 初始化 ATM 对象。
void deposit(int[] banknotesCount) 分别存入 $20 ,$50,$100,$200 和 $500 钞票的数目。
int[] withdraw(int amount) 返回一个长度为 5 的数组,分别表示 $20 ,$50,$100 ,$200 和 $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 张 $100 ,2 张 $200 和 1 张 $500 的钞票。
atm.withdraw(600); // 返回 [0,0,1,0,1] 。机器返回 1 张 $100 和 1 张 $500 的钞票。机器里剩余钞票的数量为 [0,0,0,2,0] 。
atm.deposit([0,1,0,1,1]); // 存入 1 张 $50 ,1 张 $200 和 1 张 $500 的钞票。
// 机器中剩余钞票数量为 [0,1,0,3,1] 。
atm.withdraw(600); // 返回 [-1] 。机器会尝试取出 $500 的钞票,然后无法得到剩余的 $100 ,所以取款请求会被拒绝。
// 由于请求被拒绝,机器中钞票的数量不会发生改变。
atm.withdraw(550); // 返回 [0,1,0,0,1] ,机器会返回 1 张 $50 的钞票和 1 张 $500 的钞票。
提示:
banknotesCount.length == 5
0 <= banknotesCount[i] <= 109
1 <= amount <= 109
总共 最多有 5000 次 withdraw 和 deposit 的调用。
函数 withdraw 和 deposit 至少各有 一次 调用。
解题方法1
#include <iostream>
#include <vector>
#include <tuple>
#include <map>
using namespace std;
class ATM {
private:
vector<int> table {20 ,50, 100, 200, 500};
map<int, int> idx_map {{20, 0}, {50, 1}, {100, 2}, {200, 3}, {500, 4}};
vector<int> data = {0, 0, 0, 0, 0};
public:
ATM() {
}
void deposit(vector<int> banknotesCount) {
data[0] += banknotesCount[0];
data[1] += banknotesCount[1];
data[2] += banknotesCount[2];
data[3] += banknotesCount[3];
data[4] += banknotesCount[4];
}
vector<int> withdraw(int amount) {
vector<int> needs(data.size());
for (int i = data.size() - 1; i >= 0; i--) {
int n = amount / table[i];
needs[i] = min(n, data[i]);
amount -= needs[i] * table[i];
}
if (amount > 0) {
return {-1};
}
// 取钱
for (int i = 0; i < data.size(); i++) {
data[i] -= needs[i];
}
return needs;
}
};
void printVec(const vector<int>& bookVec) {
for (int i = 0; i < bookVec.size(); ++i) {
cout << bookVec[i] << " ";
}
cout << endl;
}
int main(int argc, char **argv) {
ATM atm;
atm.deposit({0,0,1,2,1}); // 存入 1 张 $100 ,2 张 $200 和 1 张 $500 的钞票。
vector<int> ret1 = atm.withdraw(600); // 返回 [0,0,1,0,1] 。机器返回 1 张 $100 和 1 张 $500 的钞票。机器里剩余钞票的数量为 [0,0,0,2,0] 。
printVec(ret1);
atm.deposit({0,1,0,1,1}); // 存入 1 张 $50 ,1 张 $200 和 1 张 $500 的钞票。
// 机器中剩余钞票数量为 [0,1,0,3,1] 。
vector<int> ret2 = atm.withdraw(600); // 返回 [-1] 。机器会尝试取出 $500 的钞票,然后无法得到剩余的 $100 ,所以取款请求会被拒绝。
printVec(ret2);
// 由于请求被拒绝,机器中钞票的数量不会发生改变。
vector<int> ret3 = atm.withdraw(550); // 返回 [0,1,0,0,1] ,机器会返回 1 张 $50 的钞票和 1 张 $500 的钞票。;
printVec(ret3);
return 0;
}
输出:
0 0 1 0 1
-1
0 1 0 0 1
解题方法2(优化size获取方式, 提升性能)
#include <iostream>
#include <vector>
#include <tuple>
#include <map>
using namespace std;
class ATM {
private:
static constexpr int size = 5;
vector<int> table {20 ,50, 100, 200, 500};
vector<int> data;
public:
ATM(): data(size, 0) { // 构造函数阶段完成初始化
}
void deposit(vector<int> banknotesCount) {
for (int i =0; i < size; i++) {
data[i] += banknotesCount[i];
}
}
vector<int> withdraw(int amount) {
vector<int> needs(size, 0);
for (int i = size - 1; i >= 0; i--) {
int n = amount / table[i];
needs[i] = min(n, data[i]);
amount -= needs[i] * table[i];
}
if (amount > 0) {
return {-1};
}
// 取钱
for (int i = 0; i < size; i++) {
data[i] -= needs[i];
}
return needs;
}
};