Beats 100% of users with C++ || Using Map or Using Counting Approach || Best Solution ||
Intuition\n Describe your first thoughts on how to solve this problem. \n\nif you like the approach please upvote it\n\n\n\n\n\n\nif you like the approach pleas
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nif you like the approach please upvote it\n\n\n\n![image.png](https://assets.leetcode.com/users/images/7495fea3-7639-48a4-8ad9-79dc9237db4e_1701793058.7524364.png)\n\n\nif you like the approach please upvote it\n\n\n\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThere Are two Approach that i use for solving this question \n1 :- Hash map ( ordered map )\n2 :- Using counting \n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->O( N )\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->O ( 1 )\n\n# Code\n```\nclass Solution {\npublic:\n    bool lemonadeChange(vector<int>& bills) \n    {\n        map<int,int>mp;\n        for(int i=0;i<bills.size();i++)\n        {\n            if(bills[i]==5)\n                mp[5]++;\n            else if(bills[i]==10)\n            {\n                mp[10]++;\n                if(mp[5]>0)\n                    mp[5]--;\n                else\n                    return false;\n            }\n            else if(bills[i]==20)\n            {\n                mp[20]++;\n                if(mp[10]>0 && mp[5]>0)\n                {\n                    mp[10]--;\n                    mp[5]--;\n                }\n                else if(mp[5]>2)\n                {\n                    mp[5]=mp[5]-3;\n                }\n                else\n                    return false;\n            }\n        }\n        return true;\n    }\n};\n```\n#code\n```\nclass Solution {\npublic:\n    bool lemonadeChange(vector<int>& bills) \n    {\n        int five = 0, ten = 0;\n        for (int i = 0; i < bills.size(); i++)\n        {\n            if (bills[i] == 5)\n                five++;\n            else if (bills[i] == 10)\n            {\n                if (five > 0)\n                {\n                    five--;\n                    ten++;\n                }\n                else\n                    return 0;\n            } \n            else\n            {\n                if (five > 0 && ten > 0) \n                {\n                    five--;\n                    ten--;\n                } \n                else if (five >= 3) \n                {\n                    five -= 3;\n                }\n                else\n                    return 0;\n            }\n        }\n        return 1;\n    }\n};
['Array', 'Hash Table', 'Greedy', 'Ordered Map', 'Counting', 'C++']