860 LeetCode lemonade give change

Topic Description:
Here Insert Picture 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;
    }
};
Published 133 original articles · won praise 0 · Views 1131

Guess you like

Origin blog.csdn.net/peachzy/article/details/104472969