Lemonade Change

Pattern: Candy and assign cookies

easy

Lemonade costs 5 and customers pay with 5, 10 or 20. Keep counts of your 5 and 10 notes. For a 10, give back a 5. For a 20, give back a 10 and a 5 if you can, since 5s are more useful later; otherwise three 5s. If you cannot, return false.

The problem

Each lemonade costs 5. Customers come in order and pay with one note: 5, 10 or 20. You start with no change. Return whether you can give every customer the right change. [5, 5, 5, 10, 20] is true; [5, 5, 10, 10, 20] is false.

Think first

  • What change does a 10 need? A 20?
  • Which note is more flexible to keep?
  • Is there ever a choice for a 10?

Hints

    Brute force, and why it fails

    Try both ways of changing every 20: exponential in the number of 20s.

    The key insight

    Giving a 10 whenever possible keeps the 5s, which can do everything a 10 can and more, so the greedy choice never hurts.

    Dry run

    five3
    ten0

    bills [5, 5, 5, 10, 20]. Three 5s: five = 3.

    Step 1 of 3

    The plan

    for b: if b == 5: five += 1
           elif b == 10: need a five
           else: use ten + five if possible, else three fives, else return false
    

    Complexity

    Time
    O(n)
    Space
    O(1)

    One pass with two counters.

    Edge cases

    • The first customer pays with 10: false.
    • Many 20s in a row.
    • Only 5s: true.
    I've built my logic, show the code

    Java

    class Solution {
        public boolean lemonadeChange(int[] bills) {
            int five = 0, ten = 0;
            for (int b : bills) {
                if (b == 5) five++;
                else if (b == 10) {
                    if (five == 0) return false;
                    five--;
                    ten++;
                } else {                            // a 20: give 10 + 5 if possible, keeping fives for later
                    if (ten > 0 && five > 0) { ten--; five--; }
                    else if (five >= 3) five -= 3;
                    else return false;
                }
            }
            return true;
        }
    }

    C++

    class Solution {
    public:
        bool lemonadeChange(vector<int>& bills) {
            int five = 0, ten = 0;
            for (int b : bills) {
                if (b == 5) five++;
                else if (b == 10) {
                    if (five == 0) return false;
                    five--;
                    ten++;
                } else {                            // a 20: give 10 + 5 if possible, keeping fives for later
                    if (ten > 0 && five > 0) { ten--; five--; }
                    else if (five >= 3) five -= 3;
                    else return false;
                }
            }
            return true;
        }
    };

    Python

    class Solution:
        def lemonadeChange(self, bills: list[int]) -> bool:
            five = ten = 0
            for b in bills:
                if b == 5:
                    five += 1
                elif b == 10:
                    if five == 0:
                        return False
                    five, ten = five - 1, ten + 1
                elif ten and five:                  # a 20: give 10 + 5 if possible, keeping fives for later
                    ten, five = ten - 1, five - 1
                elif five >= 3:
                    five -= 3
                else:
                    return False
            return True

    Common mistakes

    • Giving three 5s when a 10 and a 5 are available.
    • Counting 20s as change; they are never given back.

    Similar problems