Topic Description:
idea: For every customer's money judgment; the number of the next change using a hash table records
code show as below:
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
map<int,int>change;
for(int i=0;i<bills.size();i++){
if(bills[i]==5)
change[5]++;
if(bills[i]==10){
if(change[5]>0){
change[5]--;
change[10]++;
}
else return false;
}
if(bills[i]==20){
if(change[10]>0){
if(change[5]>0){
change[10]--;
change[5]--;
change[20]++;
}
else return false;
}
else if(change[5]>=3){
change[5]-=3;
change[20]++;
}
else return false;
}
}
return true;
}
};