Easy C++ Solution with Video Explanations
Video Explanation\nhttps://youtu.be/pqM-asL-4rs\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem involves handling a se
# Video Explanation\nhttps://youtu.be/pqM-asL-4rs\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem involves handling a sequence of payments with `$5`, `$10`, and `$20` bills, while giving change for each payment according to the customers bill. We must ensure that we always have enough change in hand to return to the customer. The main challenge lies in effectively managing `$5 `and `$10` bills to ensure we can cover the cost of higher denomination payments (`$10` or`$20`).\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize Variables: We need two counters, five and ten, to track the number of `$5` and `$10` bills we currently have.\n2. Iterate through the bills:\n- For each `$5` bill, we simply increment the five counter, as no change is required.\n- For each `$10` bill, we decrement the five counter (if we have any) because we need to give back `$5` as change and then increment the ten counter.\n- For each `$20` bill, we try to give change using one `$10` bill and one `$5` bill first (as this preserves more `$5` bills). \n- If that isn\'t possible, we  check if we can give change using three `$5` bills instead. \n- If neither option works, the transaction fails, and we return false.\n3. Final Check: If we successfully process all the bills, return true.\n\n# Complexity\n- Time complexity:$$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    bool lemonadeChange(vector<int>& bills) {\n        int five=0;\n        int ten=0;\n        for(int i=0;i<bills.size();i++){\n            if(bills[i]==5){five++;}\n            else if(bills[i]==10){\n                if(five==0)return false;\n                five--;\n                ten++;\n            }else{\n                if(ten>0 && five>0){\n                    ten--;\n                    five--;\n                }else if(five>=3){\n                    five-=3;\n                }else{\n                    return false;\n                }\n            }\n        }\n        return true;\n    }\n};\n```
['C++']