✅Easy and Clean Code (C++ / Java)✅
Intuition:\n\n- Smallest Denomination First: When trying to give change, prefer to give out the largest bills first (e.g., give a $10 bill when possible) becaus
### Intuition:\n\n- **Smallest Denomination First:** When trying to give change, prefer to give out the largest bills first (e.g., give a $10 bill when possible) because this leaves more $5 bills available for future transactions. This strategy maximizes your chances of being able to provide the correct change in subsequent transactions.\n  \n- **Tracking State Efficiently:** The use of a map (or an array for a small number of denominations) is efficient for tracking the number of each bill. This approach ensures that each lookup and update operation is done in constant time, making the solution efficient even for large inputs.\n\n- **Edge Cases:** The solution naturally handles edge cases such as running out of $5 bills early or being unable to provide change for a $20 bill. The early return of `false` ensures that you stop processing as soon as you can\'t meet the requirements, making the function efficient.\n\nThis approach ensures that you can handle a variety of payment scenarios while minimizing the chances of running out of the necessary bills to provide change.\n\n### Approach:\n\n1. **Initialize Tracking Variables:**\n   - Use a `HashMap` (or in C++, an `unordered_map`) to track the number of $5 and $10 bills you have. This map will help you determine whether you can provide the correct change when customers pay with larger bills.\n   \n2. **Iterate Over the Bills:**\n   - Loop through each bill in the `bills` array (or vector in C++).\n   - For each bill, calculate the change needed by subtracting $5 from the bill value (since each lemonade costs $5).\n\n3. **Handle Different Cases:**\n   - **Case 1:** The bill is $5:\n     - No change is needed. Simply increment the count of $5 bills in the map.\n   - **Case 2:** The bill is $10:\n     - The customer needs $5 as change. Check if there\'s a $5 bill available in the map.\n     - If yes, decrement the count of $5 bills and add the $10 bill to the map.\n     - If no, return `false` because you can\'t provide the correct change.\n   - **Case 3:** The bill is $20:\n     - The customer needs $15 as change. This can be given in two ways:\n       1. **Preferred:** One $10 bill and one $5 bill. Check if both are available.\n       2. **Fallback:** Three $5 bills if the first option is not possible.\n     - If neither option is possible, return `false`.\n\n4. **Return Result:**\n   - If the loop completes without returning `false`, it means you were able to provide the correct change for all customers. Return `true`.\n\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)$$ -->\nbecause HashMap will always take size 3 only it does not depends on N\nwhere N is size of bills.\n# Code\n```C++ []\nclass Solution {\npublic:\n    bool lemonadeChange(vector<int>& bills) {\n        unordered_map<int, int> track;\n        int netBal = 0;\n        for(auto bill : bills) {\n            int  change = bill - 5;\n            if (change == 5) {\n                if(track[5] == 0) return false;\n                else track[5]--;\n            } \n            else if (change == 15) {\n                if (track[5] >= 1 && track[10] >= 1) {\n                    track[5]--;\n                    track[10]--;\n                }\n                else if (track[5] >= 3) track[5] += -3;\n                else return false;\n            }\n            track[bill]++;\n        }\n        return true;\n    }\n};\n```\n```Java []\nclass Solution {\n    public boolean lemonadeChange(int[] bills) {\n        Map<Integer, Integer> track = new HashMap<>();\n\n        for (int bill : bills) {\n            int change = bill - 5;\n\n            if (change == 5) {\n                if (track.getOrDefault(5, 0) == 0) {\n                    return false;\n                } else {\n                    track.put(5, track.get(5) - 1);\n                }\n            } else if (change == 15) {\n                if (track.getOrDefault(5, 0) >= 1 && track.getOrDefault(10, 0) >= 1) {\n                    track.put(5, track.get(5) - 1);\n                    track.put(10, track.get(10) - 1);\n                } else if (track.getOrDefault(5, 0) >= 3) {\n                    track.put(5, track.get(5) - 3);\n                } else {\n                    return false;\n                }\n            }\n\n            track.put(bill, track.getOrDefault(bill, 0) + 1);\n        }\n\n        return true;\n    }\n}\n\n```\n![upvote.jpeg](https://assets.leetcode.com/users/images/62c9334c-eb91-4c25-944c-61fb91edd0cb_1723695770.8183305.jpeg)\n\n\n
['Array', 'Greedy', 'C++', 'Java']