💯✅Simple and effective solution | Python, C++ and Java Solution | Upvote pliss✅💯
Result (Python):\n\n\n# Result (C++):\n\n# Result (Java):\n\n\n# \uD83D\uDCCB Problem Description:\n\nThe lemonadeChange function is designed to determine if it
# Result (Python):\n![image.png](https://assets.leetcode.com/users/images/82a5efa4-08ae-4fa6-a712-a65c77c10d53_1723698644.7050314.png)\n\n# Result (C++):\n![image.png](https://assets.leetcode.com/users/images/9beb13ba-91f3-4ea5-a0b5-a8573c0c4a7f_1723698569.0568042.png)\n# Result (Java):\n![image.png](https://assets.leetcode.com/users/images/a9e243cc-2abc-421b-ab4b-f8254fa49ad4_1723698630.7181263.png)\n\n# \uD83D\uDCCB Problem Description:\n\nThe lemonadeChange function is designed to determine if it\'s possible to provide correct change to every customer in a line at a lemonade stand. Customers pay with $$5$$, $$10$$, or $$20$$ dollar bills, and the goal is to check if you can give the correct change back using the money you\'ve collected so far, assuming you start with no money.\n\n# \uD83E\uDDE9 Code Explanation:\n\n1. **Main Function: lemonadeChange:**\n``` Python []\ndef lemonadeChange(self, bills):\n    fives, tens = 0, 0\n```\n\n- Variables Initialization:\n    - fives keeps track of the number of $$5$$ dollar bills collected.\n    - tens keeps track of the number of $$10$$ dollar bills collected.\n\n2. **Loop Through Customer Payments:**\n``` Python []\nfor i in bills:\n    if i == 5:\n        fives += 1\n```\n\n- Payment of 5 dollar:\n    - If a customer pays with a 5 dollar bill, no change is required, so you simply increase the count of fives by one.\n``` Python []\n    elif i == 10:\n        if fives == 0:\n            return False\n        fives -= 1\n        tens += 1\n```\n\n- **Payment of 10 dollar**:\n    - If a customer pays with a $$10$$ dollar bill, you need to give back $$5$$ dollar in change.\n    - **Check Availability:** If there are no $$5$$ dollar bills available (`fives` == $$0$$), you cannot provide the correct change, so the function returns **False**.\n    - **Give Change:** If you have a $$5$$ dollar bill, you decrease the count of fives by one and increase the count of tens by one (since you now have a $$10$$ dollar bill).\n``` Python []\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```\n\n- **Payment of $$20$$ dollar:**\n    - If a customer pays with a $$20$$ dollar bill, you need to give back $$15$$ dollar in change.\n    - **Option 1:** If you have both a $$10$$ dollar bill and a $$5$$ dollar bill, you give one of each as change. This is the preferred method because it conserves more $$5$$ dollar bills.\n    - **Option 2:** If you don\u2019t have a $$10$$ dollar bill but have at least three $$5$$ dollar bills, you give three $$5$$ dollar bills as change.\n    - **Fail Condition:** If neither option is available, you cannot provide the correct change, so the function returns False.\n\n3. **Return Statement:**\n``` Python []\nreturn True\n```\n- **Final Output:**\n    - If you successfully provide the correct change to every customer in the line, the function returns True.\n\n## \uD83D\uDD70\uFE0F Time Complexity: $$O(n)$$\n\n**Time Complexity:** The function iterates through the bills list once, making the time complexity $$O(n)$$, where `n` is the number of customers (or the length of the bills list).\n\n## \uD83D\uDCBE Space Complexity: $$O(1)$$\n\n**Space Complexity:** The space complexity is $$O(1)$$ since only a fixed amount of extra space (for the `fives` and `tens` counters) is used, regardless of the input size.\n\n## \uD83E\uDDE9 Example Walkthrough:\n\nConsider the `input` **bills** = $$[5, 5, 5, 10, 20]$$:\n\n- **First Customer:**\n    - Pays with $$5$$ dollars. **No change needed**. `fives` = $$1$$, `tens` = $$0$$.\n- **Second Customer:**\n    - Pays with $$5$$ dollars. No change needed. `fives` = $$2$$, `tens` = $$0$$.\n- **Third Customer:**\n    - Pays with $$5$$ dollars. No change needed. `fives` = $$3$$, `tens` = $$0$$.\n- **Fourth Customer:**\n    - Pays with $$10$$ dollars. Change needed: $$5$$ dollars.\n    - Use one $$5$$ dollar bill. `fives` = 2, `tens` = 1.\n- **Fifth Customer:**\n    - Pays with $$20$$ dollars. Change needed: $$15$$ dollars.\n    - Use one $$10$$ dollars bill and one $$5$$ dollars bill. `fives` = $$1$$, `tens` = $$0$$.\n\nSince all customers were provided with the correct change, the function returns **True**.\n\n# \uD83D\uDCBBCode:\n``` Python []\nclass Solution(object):\n    def lemonadeChange(self, bills):\n        fives, tens = 0, 0\n\n        for i in bills:\n            if i == 5:\n                fives += 1\n            elif i == 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            \n        return True\n```\n``` C++ []\nclass Solution {\npublic:\n    bool lemonadeChange(vector<int>& bills) {\n        int fives = 0, tens = 0;\n\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``` Java []\nclass Solution {\n    public boolean lemonadeChange(int[] bills) {\n        int fives = 0, tens = 0;\n\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> # Upvote and make the kitty happy\uD83D\uDE0A\n![catto meme.webp](https://assets.leetcode.com/users/images/964a2143-fb0f-4119-b995-5be0bcced4e1_1723699451.0607955.webp)\n
['Array', 'Greedy', 'Simulation', 'Python', 'C++', 'Java', 'Python3']