Lemonade Change

Decide whether a lemonade stand that starts with an empty till can give every customer correct change.

A cup of lemonade costs $5. Customers buy one cup each, in the order given by the array bills, and each pays with a single bill of $5, $10 or $20. You start with no money and must give every customer the correct change. Return true if you can, and false otherwise.

Example 1
Input: bills = [5, 5, 5, 10, 20]
Output: true
Example 2
Input: bills = [5, 5, 10, 10, 20]
Output: false
Explanation: after the two $10 bills you hold two tens and no fives, and $15 cannot be made from tens.
Solution

The Idea

Picture a small lemonade stand with a till that starts empty. The only money you can hand back as change is money that earlier customers paid you, so the order of the line matters.

Walk through Example 1, [5, 5, 5, 10, 20]. The first three customers pay exactly $5, and the till holds three fives. The fourth pays $10 and gets one five back, which leaves two fives and one ten. The fifth pays $20 and needs $15: the ten and one five cover it, so everyone is served.

Example 2, [5, 5, 10, 10, 20], goes wrong at the end. The two $10 customers each take a five, so when the $20 customer arrives the till holds two tens and no fives. Two tens make $20, not $15, and the answer is false.

Notice that a $20 bill is never handed back, because the most change anyone needs is $15. So the till comes down to two numbers: how many fives and how many tens. A $5 or $10 customer leaves you no choice. A $20 customer does, since $15 is either a ten and a five, or three fives. Which should you hand over?

Look at the picture below. The customers pay $5, $5, $5, $5, $10, $20 and $10, and each box shows the till after that customer. Up to the $20, both rows are the same: three fives and one ten.

Customers pay 5, 5, 5, 5, 10, 20 and 10 dollars. The row labelled ten + five pays the 20 with a ten and a five and ends with one five and one ten. The row labelled three fives pays the 20 with three fives and has no five for the last 10 dollar customer.

The row labelled "ten + five" pays the $20 with a ten and a five, and keeps two fives. The row labelled "three fives" keeps the ten instead and is left with no fives at all. When the last customer pays $10, only the first row has a five to give back; the second row ends in the box "no five for change".

The difference is what each bill can still do later. A five can be change for a $10 or for a $20, while a ten only helps with a $20. So when a $20 customer gives you the choice, hand over the ten and keep the fives. Picking the bills to hand back by what looks best right now, without trying out the customers still in line, is called a greedy choice.

Solution Approach

Turning that idea into code means settling three things: what to remember about the till, how to answer each bill, and why paying a $20 with ten + five is safe, which spares us from trying both choices.

What to remember about the till

Do we need the list of bills in the till? No, only their counts. Two counters are enough: fives for the $5 bills and tens for the $10 bills. A $20 bill can never be given back, so there is no counter for it.

Answering each bill

What happens to the counters for each kind of bill? A $5 bill adds one to fives. A $10 bill needs one five back, so it fails when fives is 0, and otherwise takes one five and adds one ten. A $20 bill pays with a ten and a five if the till has both, with three fives if it has no ten, and fails if neither works.

Why ten + five is the safe choice

Could keeping the ten ever pay off later? Compare the two tills right after a $20 customer. Paying with ten + five leaves two more fives and one fewer ten than paying with three fives. Any later change that would use that ten can use the two fives instead, since together they are also $10.

So the ten + five till can serve every line of customers that the three-fives till can serve, and sometimes more, as the picture showed. Without that argument, the safe way would be to try both choices at every $20 customer. Each $20 would double the number of tills to follow, and 30 such customers would already give over a billion.

Java Solution

The method lemonadeChange works in these steps:

  1. First, set two counters, fives and tens, to 0. They hold how many $5 and $10 bills are in the till.
  2. A for loop takes the customers one at a time, in order, with the bill each one pays in bill.
  3. For a $5 bill, add one to fives.
  4. For a $10 bill, return false if fives is 0. Otherwise take one five out and add one to tens.
  5. For a $20 bill, first check tens > 0 && fives > 0 and take one of each. If that fails, check fives >= 3 and take three fives. If both fail, return false.
  6. If the loop gets through every customer, everyone got change, so return true.

The main method runs both examples, a first customer who pays $10, a $20 that only three fives can pay for, and the line of customers from the picture.

Java
public class LemonadeChange {

    public static boolean lemonadeChange(int[] bills) {
        int fives = 0;
        int tens = 0;
        for (int bill : bills) {
            if (bill == 5) {
                fives++;                      // no change needed, keep the five
            } else if (bill == 10) {
                if (fives == 0) {
                    return false;             // $5 change, but no five in the till
                }
                fives--;
                tens++;
            } else {                          // a $20 bill: give back $15
                if (tens > 0 && fives > 0) {
                    tens--;                   // ten + five: keep the fives for later
                    fives--;
                } else if (fives >= 3) {
                    fives -= 3;               // no ten left, so three fives
                } else {
                    return false;
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(lemonadeChange(new int[] {5, 5, 5, 10, 20}));
        System.out.println(lemonadeChange(new int[] {5, 5, 10, 10, 20}));
        System.out.println(lemonadeChange(new int[] {10}));
        System.out.println(lemonadeChange(new int[] {5, 5, 5, 20}));
        System.out.println(lemonadeChange(new int[] {5, 5, 5, 5, 10, 20, 10}));
    }
}

Running it prints:

Output
true
false
false
true
true

Example 1 gives true on the first line, and Example 2 gives false on the second, at the $20 customer who meets a till of two tens. The third line is false too, but much sooner: the very first customer pays $10 into an empty till.

The fourth line, true, comes from [5, 5, 5, 20]. The till has no ten when the $20 arrives, so the code falls through to the three-fives branch. The last line is the picture's line of customers, and it prints true. With the two $20 branches swapped, so that three fives are tried first, the same input returns false, because the last $10 customer finds no five.

Complexity

The method serves each customer once and keeps nothing but the two counters.

  • Time: O(n), because the loop looks at each of the n customers' bills once and does at most a few comparisons and counter updates for each.
  • Space: O(1), because fives and tens describe the whole till, however long the line of customers grows.

Spending the bill that is good for less and saving the one that is good for more is also the rule behind Assign Cookies, where each child gets the smallest cookie that is big enough.