Lemonade Change (LeetCode 860): Solution and Explanation

Easy
Array
Greedy

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

  1. Set five = ten = 0.
  2. For a $5 bill, add it to five.
  3. For a $10 bill, return false if five == 0; otherwise give back a $5 (five -= 1) and keep the $10 (ten += 1).
  4. For a $20 bill, if ten > 0 and five > 0, give back $10 + $5. Else if five >= 3, give back three $5 bills. Else return false.
  5. If every customer is served, return true.

Step-by-step example

bills = [5,5,5,10,20]
  1. Customers 0, 1 and 2 pay $5 each: five = 3, ten = 0.
  2. Customer 3 pays $10 and is owed $5: five = 2, ten = 1.
  3. Customer 4 pays $20 and is owed $15. A $10 and a $5 are available, so give those: five = 1, ten = 0.
  4. 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 True
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each customer is handled once with a constant amount of work.

Space

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 >= 3 before ten > 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.