题目:
代码(首刷自解 2024年2月15日):
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
vector<int> leftchange(2,0);//leftchange[0]代表5元数量,1代表10
for (int i = 0; i < bills.size(); ++i) {
if (bills[i] == 5) leftchange[0]++;
else if (bills[i] == 10) {
leftchange[1]++;
if (leftchange[0] == 0) return false;
else leftchange[0]--;
} else {//顾客付了20
if (leftchange[1] > 0) {
leftchange[1]--;
if (leftchange[0] == 0) return false;
else leftchange[0]--;
} else {
if (leftchange[0] < 3) return false;
else leftchange[0] = leftchange[0] - 3;
}
}
}
return true;
}
};