使用C++进行编码:
bool lemonadeChange(vector<int>& bills) { int FIVE = 0; int TEN = 0; int TWEN = 0; int N = bills.size(); for (int i = 0; i < N; i++) { int p = bills[i]; if (p == 5) { FIVE++; } else if (p == 10) { TEN++; FIVE--; } else if (p == 20) { TWEN++; if (TEN > 0) { TEN--; FIVE--; } else { FIVE -= 3; } } if (FIVE < 0 || TEN < 0 || TWEN < 0) { return false; } } return true; }