💡 C++ | O(n) | Simple Logic | With Full Explanation ✏️
Intuition\n- Track the number of 5 and 10 bills.\n- Always try to give change using higher denomination bills (i.e., prioritize giving a 10 bill over three 5 bi
# Intuition\n- Track the number of 5 and 10 bills.\n- Always try to give change using higher denomination bills (i.e., prioritize giving a 10 bill over three 5 bills when handling 20 payments).\n- Return false as soon as you find that you cannot provide the correct change for a customer.\n\n# Approach\nWe will keeps track of the number of 5 and 10 bills available using two variables, `change5` and `change10`. \n\n**For each customer :**\n- if they pay with a 5 bill, no change is needed, so the count of 5 bills is simply increased. \n- If they pay with a 10 bill, a 5 bill must be returned as change. If a 5 bill is available, it is given as change; otherwise, the transaction is impossible, and the function returns `false`. \n- For a 20 bill, the preferred strategy is to give one 10 bill and one 5 bill as change (since this conserves 5 bills for future transactions). If this is not possible, three 5 bills are given instead. If neither option is available, the function returns `false`. \n\nIf all transactions are successful, the function returns `true`, indicating that correct change was provided to every customer. It\nensures that we maximize the number of smaller denomination bills, which are essential for giving change.\n\n# Complexity\n- Time complexity:\n```O(n)```\n\n- Space complexity:\n```O(1)```\n\n# Code\n```\n#pragma GCC optimize("Ofast")  // Compiler directive for code optimization\n\nclass Solution {\npublic:\n    bool lemonadeChange(vector<int>& bills) {\n        // Optimize input/output operations\n        cin.tie(0);\n        cout.tie(0);\n        ios::sync_with_stdio(false);\n\n        int change5 = 0;  // Number of $5 bills available\n        int change10 = 0; // Number of $10 bills available\n\n        // Loop through each customer\'s bill in the bills vector\n        for (int i : bills) {\n            \n            // Case when the customer pays with a $5 bill\n            if (i == 5) {\n                change5++;  // Increase the count of $5 bills\n            }\n            \n            // Case when the customer pays with a $10 bill\n            else if (i == 10) {\n                change10++;  // Increase the count of $10 bills\n                \n                // Check if we have a $5 bill to give as change\n                if (change5 > 0) {\n                    change5--;  // Give one $5 bill as change\n                } else {\n                    return false;  // If no $5 bill is available, return false\n                }\n            }\n            \n            // Case when the customer pays with a $20 bill\n            else {\n                // Prefer to give one $10 bill and one $5 bill as change\n                if (change10 > 0 && change5 > 0) {\n                    change10--;  // Give one $10 bill\n                    change5--;   // Give one $5 bill\n                }\n                // If no $10 bill is available, give three $5 bills as change\n                else if (change5 >= 3) {\n                    change5 -= 3;  // Reduce three $5 bills\n                } \n                // If we can\'t provide the correct change, return false\n                else {\n                    return false;\n                }\n            }\n        }\n        \n        // If we were able to provide correct change for all customers, return true\n        return true;\n    }\n};\n\n```\n\n\n\n![anonupvote.jpeg](https://assets.leetcode.com/users/images/bf7ce48f-1a34-404c-8610-410d43112c2c_1720413559.1746094.jpeg)\n\n# Ask any doubt !!!\nReach out to me \uD83D\uDE0A\uD83D\uDC47\n\uD83D\uDD17 https://tusharbhardwa.github.io/Portfolio/\n\n\n
['C++']