Using Map in C++ with a single for loop
Intuition\n Describe your first thoughts on how to solve this problem. \nImagine, you have a shop of lemonade costs 5.\nIn the beginning of the day, you have on
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nImagine, you have a shop of lemonade costs 5.\nIn the beginning of the day, you have only lemonade NO MONEY in your drawer of shop(it is written in the question).\nNow, the first customer comes, he gives you 5 and in return you give him the lemonade. (Drawer is having 5)\nThen, second customer comes up with 10 and you served him a lemonade and then in return you gave him 5 as he is having 10 and the cost of a lemonade is 5 (here, you will check whether you have one 5 dollar note or not, if not then return false).\nThen a customers comes up with 20 dollar and you served him a lemonade which cost 5 dollar. Now think, you have 20 dollar and return for your betterment you will give him 5 dollar\'s 3 notes or one 10 dollar and one 5 dollar note? You will give him one 10 and 5 dollar note if you have and if you don\'t have then three 5 dollar notes.\nThat\'s it, now your business will run on the sky. LOL.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirst use a map to store the key as 5,10,20 dollar and increment or decrement it\'s value as per the condition.\nFirst check, if the customer have dollar 5, if yes increment the value of key 5 in the map.\nSecond, check if the customer have 10 dollar note, if yes then increment the value of key 10 and decrement the value of key 5 as you will give him back  Dollar 5 return of 10 dollar for the lemonade.\nThird, check if the customer have 15 dollar note, if he is having 20 dollar then first check whether you have the value of key 5 and key 10 more than 0 then only increment the value of key 20 and decrement the value of key 5 and key 10 in exchange of 20 dollar with 10 and 5 dollar for the lemonade.\nIf all the conditions fails then return false. \n\n# Complexity\n- Time complexity: $$0(n)$$ \n  **Only one for loop is used.**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$ O(1) $$ \n  **constant space is used.**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    bool lemonadeChange(vector<int>& bills) {\n\n        map<int,int>KeeperMoney;\n\n        // For $5, created a key with 0 value;\n        KeeperMoney[5]=0;\n\n        // For $10, created a key with 0 value;\n        KeeperMoney[10]=0;\n\n        // For $20, created a key with 0 value;\n        KeeperMoney[20]=0;\n\n        // Now traverse over the array and increment decrement as per the requirement;\n        for(int i=0;i<bills.size();i++)\n        {\n            // First, we added $5 when the first customer came;\n            if(bills[i]==5){\n                KeeperMoney[5]++;\n            }\n            // Now, if the customer comes up with $10, then we will return them $5 as exchange.\n            else if(bills[i]==10 && KeeperMoney[5]>0){\n                // tells that we have given $5 back to the customer;\n                KeeperMoney[5]--;\n                // tells that we received $10 from the customer in exchange of $5;\n                KeeperMoney[10]++;\n            }\n            // Now, if the customer comes up with $20, then first check to give $5 and $10 together and then try to give $5 3 notes;\n            else if(bills[i]==20){\n                if(KeeperMoney[5]>0 && KeeperMoney[10]>0){\n                    // decrements $5 and $10;\n                    KeeperMoney[5]--;\n                    KeeperMoney[10]--;\n                    // Increment $20;\n                    KeeperMoney[20]++;\n                }\n                else if(KeeperMoney[5]>2){\n                    // Check if $5 is more than 2 then give $5\'s 3 notes to the customer\n                    KeeperMoney[5] -= 3;\n                    // and stores $20 notes;\n                    KeeperMoney[20]++;\n                }\n                else{\n                    return false;\n                }\n            }\n            else{\n                return false;;\n            }\n        }\n\n        return true;\n\n    }\n};\n```\n\n\n# ***Kindly Upvote this solution if like the explanation.***
['Array', 'Ordered Map', 'C++']