Lemonade Change
Lemonade Change is LeetCode 860 (Easy). Each lemonade costs $5. Customers buy one each, in the order given by bills, and pay with a $5, $10 or $20 bill. You start with no money in the till.
- Each customer must get exact change straight away, using only bills you have already taken in.
- Return
trueif every customer gets correct change, andfalseotherwise.
- 1 <= bills.length <= 10⁵
- bills[i] is either 5, 10, or 20.
Intuition
Only two numbers matter: how many $5 bills and how many $10 bills you hold. A $20 is never handed back, because nobody is ever owed more than $15.
- $5: keep it.
- $10: give back one $5. There is no other choice.
- $20: you owe $15, either one $10 and one $5 or three $5s.
The only real decision is the $20, and the rule is: use the $10 first. A $10 can only ever help a later $20, while a $5 helps both a later $10 and a later $20, so spending the $10 never leaves you worse off.
This problem walks through events in order and asks whether you ever get stuck. When one option keeps strictly more choices open for the future, taking it every time is a safe greedy rule.
Approach
Before reading on, decide what state you need to track after each customer; it is smaller than you might think. Then list the ways to make $15 and decide which one to prefer. Aim for O(n) time and O(1) space.
Keep two counters
Start with five = 0 and ten = 0. These two numbers are the whole state of the till, so one pass over bills is enough.
Serve a $5 or a $10
A $5 adds one to five. A $10 needs one $5 back: if five is 0, return false at once, because a customer who can't be paid now can't be fixed by later ones. Otherwise take one from five and add one to ten.
Serve a $20 with the $10 first
If you hold a $10 and a $5, hand those back. Otherwise, if you hold at least three $5s, hand back three. If neither works, return false.
Why the $10-first rule is safe
Suppose some plan pays a $20 with three $5s while a $10 was available. Switch to $10 + $5: you now hold one $10 fewer but two $5s more. If that plan ever hands out the $10 later, you hand out two of your spare $5s instead, so you are never stuck where it is not.
Return true at the end
If the loop finishes, every customer was served with bills that were actually in the till, so return true.
Code in Python, C++ and Java
Common pitfalls
Trying three $5s before the $10
elif five >= 3:
five -= 3
elif ten > 0 and five > 0:
ten -= 1
five -= 1elif ten > 0 and five > 0:
ten -= 1
five -= 1
elif five >= 3:
five -= 3On [5,5,5,5,10,20,10] the wrong order pays the $20 with all three $5s left in the till, leaving none for the last $10, and returns false. Paying $10 + $5 keeps two $5s and the answer is true.
Tracking only the total cash
if cash < b - 5:
return False
cash += 5track five and ten separately
Holding $10 is not the same as being able to give $5. On [5,10,10] the till holds $10 when the third customer arrives, but it is a single $10 bill, so the answer is false, not true.
Not counting the $10 you take in
elif b == 10:
five -= 1elif b == 10:
five -= 1
ten += 1That $10 is what pays the next $20. On [5,5,10,20] the till really holds one $10 and one $5 for the $20, so the answer is true, but the version that forgot the $10 sees only one $5 and returns false.
Edge cases
The till is empty, so no change can be made and the function returns false on the very first bill.
Complexity
Lemonade Change FAQ
Does giving the biggest bill first always work for making change?
No. With coins of 1, 3 and 4, paying 6 greedily gives 4 + 1 + 1, three coins, while 3 + 3 uses two. The greedy rule here is safe because it covers a much narrower case: the only choice is $10 + $5 versus three $5s, and the bills are counted, not minimised.