C++/Java/Python || 🚀✅Fully Explained || 🚀✅Greedy Algo
Intuition:\nThe problem requires us to simulate the process of giving change while selling lemonade. We have three types of bills of denominations 5, 10, and 20
# Intuition:\nThe problem requires us to simulate the process of giving change while selling lemonade. We have three types of bills of denominations 5, 10, and 20. We need to keep track of the number of 5s and 10s we have at any moment to be able to give change for different bills. If at any moment, we don\'t have the required bills to give change, we return false.\n\n# Approach:\nWe use two variables fives and tens to keep track of the number of 5s and 10s we have. We initialize both of them to 0. We then iterate over the bills array and for each bill, we do the following:\n\n1. If the bill is of denomination 5, we increment the fives variable.\n2. If the bill is of denomination 10, we first check if we have at least one 5-dollar bill. If we don\'t have a 5-dollar bill, we can\'t give change, so we return false. If we have a 5-dollar bill, we decrement the fives variable and increment the tens variable.\n3. If the bill is of denomination 20, we first check if we have at least one 10-dollar bill and one 5-dollar bill. If we do, we decrement both the tens and fives variables. Otherwise, we check if\n4. we have at least three 5-dollar bills. If we do, we decrement the fives variable by 3. If we don\'t have enough bills to give change, we return false.\n5. At the end of the iteration, if we have not returned false, it means we were able to give change for all the bills and we can return true.\n\n# Complexity:\n- The time complexity of the algorithm is O(n), where n is the length of the bills array. We iterate over the array once. \n- The space complexity is O(1) because we are using only two variables fives and tens to keep track of the number of 5s and 10s we have.\n\n# Code\n# C++\n```\nclass Solution {\npublic:\n    bool lemonadeChange(vector<int>& bills) {\n        int fives = 0, tens = 0;\n        for (int bill : bills) {\n            if (bill == 5) {\n                fives++;\n            } else if (bill == 10) {\n                if (fives == 0) {\n                    return false;\n                }\n                fives--;\n                tens++;\n            } else {\n                if (tens > 0 && fives > 0) {\n                    tens--;\n                    fives--;\n                } else if (fives >= 3) {\n                    fives -= 3;\n                } else {\n                    return false;\n                }\n            }\n        }\n        return true;\n    }\n};\n\n```\n# Java\n```\nclass Solution {\n    public boolean lemonadeChange(int[] bills) {\n        int fives = 0, tens = 0;\n        for (int bill : bills) {\n            if (bill == 5) {\n                fives++;\n            } else if (bill == 10) {\n                if (fives == 0) {\n                    return false;\n                }\n                fives--;\n                tens++;\n            } else {\n                if (tens > 0 && fives > 0) {\n                    tens--;\n                    fives--;\n                } else if (fives >= 3) {\n                    fives -= 3;\n                } else {\n                    return false;\n                }\n            }\n        }\n        return true;\n    }\n}\n\n```\n# Python\n```\nclass Solution(object):\n    def lemonadeChange(self, bills):\n        fives, tens = 0, 0\n        for bill in bills:\n            if bill == 5:\n                fives += 1\n            elif bill == 10:\n                if fives == 0:\n                    return False\n                fives -= 1\n                tens += 1\n            else:\n                if tens > 0 and fives > 0:\n                    tens -= 1\n                    fives -= 1\n                elif fives >= 3:\n                    fives -= 3\n                else:\n                    return False\n        return True\n\n\n```\n# Thank You
['Array', 'Greedy', 'Python', 'C++', 'Java']