C++ | Efficient Solution for "Lemonade Change" | 55ms Beats 97.21% | Python | Java
---\n\n# Intuition\nTo determine if we can provide the correct change to every customer at a lemonade stand, we need to track the number of $5 and $10 bills we 
---\n\n# Intuition\nTo determine if we can provide the correct change to every customer at a lemonade stand, we need to track the number of `$`5 and `$`10 bills we have as we process each customer\u2019s payment. The challenge is to make sure we always have enough `$`5 and `$`10 bills to give the correct change for a `$`10 or `$`20 bill, respectively.\n\n# Approach\nWe iterate through each customer\'s payment, increasing our count of `$`5 bills if they pay with a `$`5 bill. For a `$`10 bill, we check if we have a `$`5 bill for change, if not, return false. With a `$`20 bill, we first try to give one `$`10 bill and one `$`5 bill as change. If that\'s not possible, we then attempt to give three `$`5 bills. If neither option is available, return false. This confirms we can provide the correct change to every customer if we finish processing without issues.\n\n# Complexity\n- **Time Complexity** $$O(n)$$, where $$n$$ is the length of the bills array. We only need a single pass through the array.\n- **Space Complexity** $$O(1)$$, as we only use a fixed amount of extra space for tracking the counts of `$`5 and `$`10 bills.\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    bool lemonadeChange(vector<int>& bills) {\n        int five{0}, ten{0}, i{0};\n        while (i < bills.size()) {\n            if (bills[i] == 5) five++;\n            else if (bills[i] == 10) { if (!five--) return false; ten++; }\n            else if (ten && five--) ten--;\n            else if (five >= 3) five -= 3; \n            else return false;\n            i++;\n        }\n        return true;\n    }\n};\n```\n\n```java []\nclass Solution {\n    public boolean lemonadeChange(int[] bills) {\n        int five = 0, ten = 0, i = 0;\n        while (i < bills.length) {\n            if (bills[i] == 5) five++;\n            else if (bills[i] == 10) { if (five-- <= 0) return false; ten++; }\n            else if (ten > 0 && five-- > 0) ten--;\n            else if (five >= 3) five -= 3; \n            else return false;\n            i++;\n        }\n        return true;\n    }\n}\n```\n```python []\nclass Solution:\n    def lemonadeChange(self, bills):\n        five, ten, i = 0, 0, 0\n        while i < len(bills):\n            if bills[i] == 5:\n                five += 1\n            elif bills[i] == 10:\n                if five <= 0:\n                    return False\n                five -= 1\n                ten += 1\n            elif ten > 0 and five > 0:\n                ten -= 1\n                five -= 1\n            elif five >= 3:\n                five -= 3\n            else:\n                return False\n            i += 1\n        return True\n\n```\n**Happy Independence Day![image.png](https://assets.leetcode.com/users/images/33284cb4-cafe-47fe-a0ae-16358adc5cd4_1723690300.688475.png)**\n\n---\n
['Greedy', 'Python', 'C++', 'Java']