Lemonade Change (LeetCode 860): Solution and Explanation
Each lemonade costs $5 and customers pay in order with a $5, $10 or $20 bill. You start with no money. Decide whether every customer can be given correct change.
Intuition
The only money you can hand back is money earlier customers gave you. So simulate the stand and keep a drawer.
A $20 bill is never useful as change, since no customer is ever owed $20 or more. The drawer state is just two numbers: how many $5 bills and how many $10 bills.
A $10 customer has exactly one way to be paid back: a $5. A $20 customer is owed $15, which is either $10 + $5 or three $5 bills.
When both are possible, spend the $10. A $10 bill can only ever help a future $20 customer, but a $5 bill can help both $10 and $20 customers. Keeping the more flexible bill never hurts.
Approach: Greedy cash drawer
- Set
five = ten = 0. - For a $5 bill, add it to
five. - For a $10 bill, return false if
five == 0; otherwise give back a $5 (five -= 1) and keep the $10 (ten += 1). - For a $20 bill, if
ten > 0andfive > 0, give back $10 + $5. Else iffive >= 3, give back three $5 bills. Else return false. - If every customer is served, return true.
Step-by-step example
bills = [5,5,5,10,20]
- Customers 0, 1 and 2 pay $5 each: five = 3, ten = 0.
- Customer 3 pays $10 and is owed $5: five = 2, ten = 1.
- Customer 4 pays $20 and is owed $15. A $10 and a $5 are available, so give those: five = 1, ten = 0.
- Everyone got correct change. Return true.
Code: Python, Java, C++
1class Solution:
2 def lemonadeChange(self, bills: List[int]) -> bool:
3 five = ten = 0
4 for bill in bills:
5 if bill == 5:
6 five += 1
7 elif bill == 10:
8 if five == 0:
9 return False
10 five -= 1
11 ten += 1
12 elif ten > 0 and five > 0:
13 ten -= 1
14 five -= 1
15 elif five >= 3:
16 five -= 3
17 else:
18 return False
19 return True1class Solution {
2 public boolean lemonadeChange(int[] bills) {
3 int five = 0, ten = 0;
4 for (int bill : bills) {
5 if (bill == 5) {
6 five++;
7 } else if (bill == 10) {
8 if (five == 0) {
9 return false;
10 }
11 five--;
12 ten++;
13 } else if (ten > 0 && five > 0) {
14 ten--;
15 five--;
16 } else if (five >= 3) {
17 five -= 3;
18 } else {
19 return false;
20 }
21 }
22 return true;
23 }
24}1class Solution {
2public:
3 bool lemonadeChange(vector<int>& bills) {
4 int five = 0, ten = 0;
5 for (int bill : bills) {
6 if (bill == 5) {
7 five++;
8 } else if (bill == 10) {
9 if (five == 0) {
10 return false;
11 }
12 five--;
13 ten++;
14 } else if (ten > 0 && five > 0) {
15 ten--;
16 five--;
17 } else if (five >= 3) {
18 five -= 3;
19 } else {
20 return false;
21 }
22 }
23 return true;
24 }
25};Time and space complexity
O(n)
Each customer is handled once with a constant amount of work.
O(1)
Only two counters are stored.
Other approaches
Try both ways to pay a $20 (search) (time O(2^k) for k $20 customers, space O(k))
Branch on $10 + $5 versus three $5 bills at every $20 customer and see whether any branch serves everyone. It reaches the same answer but can explode exponentially, and the greedy argument shows the branching is unnecessary.
Common mistakes
- Paying a $20 with three $5 bills when a $10 is available. On [5,5,5,5,10,20,10,10] the greedy serves everyone, but spending three $5 bills on the $20 leaves none for the next $10 customer.
- Tracking $20 bills or a single total amount of money. Having $20 in the drawer says nothing about whether you can make $5 or $15.
- Forgetting to add the $10 to the drawer after a $10 customer is served.
- Checking
five >= 3beforeten > 0 and five > 0, which reverses the greedy preference.
Frequently asked questions
Why is giving $10 + $5 always at least as good as three $5 bills?
Both leave you with the same total, but $10 + $5 leaves two more $5 bills and one fewer $10. Any future customer you could serve with the extra $10 you could also serve with two $5 bills, so the greedy drawer is never worse.
Can the stand ever need a $20 for change?
No. The most anyone is owed is $15, and $15 cannot use a $20 bill. That is why the drawer only counts $5 and $10 bills.
Do I need to process the customers in order?
Yes. The problem fixes the order of the line, and change can only come from bills already received, so the simulation must follow that order.