LeetCode #860 Easy

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 true if every customer gets correct change, and false otherwise.
Constraints
  • 1 <= bills.length <= 10⁵
  • bills[i] is either 5, 10, or 20.
greedyarray
Open on LeetCode ↗
02

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.

How to spot this pattern

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.

03

Approach

Try it first

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.

1

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.

2

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.

3

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.

4

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.

5

Return true at the end

If the loop finishes, every customer was served with bills that were actually in the till, so return true.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def lemonadeChange(self, bills):
▶3 five = ten = 0
▶4 for b in bills:
▶5 if b == 5:
▶6 five += 1
▶7 elif b == 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
customers in line$51$52$53$104$205$5 bills in the tillnone$10 bills in the tillnonechange handed back—till starts empty
five0$5 bills
ten0$10 bills
Each lemonade costs $5 and the till starts with nothing. Only the number of $5 and $10 bills matters: no customer is ever owed $20, so a $20 is never handed back.
customers in line$51$52$53$104$205$5 bills in the till$5$10 bills in the tillnonechange handed backnone neededcustomer 1 pays $5 → keep it
five1$5 bills
ten0$10 bills
A $5 pays for the lemonade exactly, so no change is due. The bill goes into the till, where it can later serve as change for a $10 or a $20.
customers in line$51$52$53$104$205$5 bills in the till$5$5$10 bills in the tillnonechange handed backnone neededcustomer 2 pays $5 → keep it
five2$5 bills
ten0$10 bills
Another exact payment. The till now holds 2 × $5, each one able to cover a later $10, or a third of a later $20.
customers in line$51$52$53$104$205$5 bills in the till$5$5$5$10 bills in the tillnonechange handed backnone neededcustomer 3 pays $5 → keep it
five3$5 bills
ten0$10 bills
Another exact payment. The till now holds 3 × $5, each one able to cover a later $10, or a third of a later $20.
customers in line$51$52$53$104$205$5 bills in the till$5$5$10 bills in the till$10change handed back$5customer 4 pays $10 → give back $5
five2$5 bills
ten1$10 bills
A $10 needs exactly one $5 back, and there is no other way to make $5. The $10 stays in the till, where it can only ever help a future $20.
customers in line$51$52$53$104$205$5 bills in the till$5$10 bills in the tillnonechange handed back$10+$5=$15customer 5 pays $20 → give $10 + $5
five1$5 bills
ten0$10 bills
$15 is due. With only 2 × $5 in the till, $10 + $5 is the only way to make it.
customers in line$51$52$53$104$205$5 bills in the till$5$10 bills in the tillnonechange handed back—all 5 customers served → true
five1$5 bills
ten0$10 bills
Every customer got exact change from bills already in the till, so the answer is true.
05

Common pitfalls

Trying three $5s before the $10

✗ Wrong
elif five >= 3:
    five -= 3
elif ten > 0 and five > 0:
    ten -= 1
    five -= 1
✓ Right
elif ten > 0 and five > 0:
    ten -= 1
    five -= 1
elif five >= 3:
    five -= 3

On [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

✗ Wrong
if cash < b - 5:
    return False
cash += 5
✓ Right
track 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

✗ Wrong
elif b == 10:
    five -= 1
✓ Right
elif b == 10:
    five -= 1
    ten += 1

That $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.

06

Edge cases

The first customer pays $10 or $20

The till is empty, so no change can be made and the function returns false on the very first bill.

07

Complexity

Time
O(n)
Space
O(1)
One pass over the n bills with constant work each. Two integer counters are the only extra memory.
08

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.