860. 柠檬水找零

 1 class Solution 
 2 {
 3 public:
 4     bool lemonadeChange(vector<int>& bills) 
 5     {
 6         unordered_map<int,int> hash;
 7         for(auto a : bills)
 8         {
 9             if(a == 5) hash[a] ++;
10             else if(a == 10)
11             {
12                 if(hash[5] >= 1) hash[5] --,hash[10] ++;
13                 else return false;
14             }
15             else
16             {
17                 if(hash[10] >= 1 && hash[5] >= 1) hash[10] --,hash[5] --,hash[20] ++;
18                 else if(hash[5] >= 3) hash[5] -= 3,hash[20] ++;
19                 else return false;
20             }
21         }
22         return true;
23     }
24 };

 

posted @ 2020-04-18 10:37  Jinxiaobo0509  阅读(147)  评论(0)    收藏  举报