Lemonade Change

Updated on 04 June, 2025
Lemonade Change header image

Problem Statement

Imagine a scenario where you operate a lemonade stand where each lemonade is priced at exactly $5. Customers form a queue to purchase lemonade from you, each buying one cup and paying with a $5, $10, or $20 bill. It's your job to make sure each customer receives the correct change, which requires you to ensure that each transaction nets exactly $5. When you start your day, you have no change in hand.

Given a sequence represented by an array bills, where bills[i] indicates the denomination of the bill paid by the i-th customer in line, the task is to determine if you can provide every customer with the correct change. If you manage to do so throughout the day, return true; otherwise, return false. This function tests your ability to manage and allocate resources efficiently under constraints.

Examples

Example 1

Input:

bills = [5,5,5,10,20]

Output:

true

Explanation:

From the first 3 customers, we collect three $5 bills in order.
From the fourth customer, we collect a $10 bill and give back a $5.
From the fifth customer, we give a $10 bill and a $5 bill.
Since all customers got correct change, we output true.

Example 2

Input:

bills = [5,5,10,10,20]

Output:

false

Explanation:

From the first two customers in order, we collect two $5 bills.
For the next two customers in order, we collect a $10 bill and give back a $5 bill.
For the last customer, we can not give the change of $15 back because we only have two $10 bills.
Since not every customer received the correct change, the answer is false.

Constraints

  • 1 <= bills.length <= 105
  • bills[i] is either 5, 10, or 20.

Approach and Intuition

Understanding how to manage change is the key to solving this problem. Let's breakdown the approach based on the scenarios provided in the examples:

  • You start with no coins. Thus, your initial task is collecting enough $5 bills to handle future transactions. A $5 bill requires no change, a $10 bill requires one $5 bill as change, and a $20 bill requires either three $5 bills or one $10 and one $5 bill as change.
  • As customers buy lemonades:
    1. If they pay with a $5 bill, simply increase your count of $5 bills.
    2. If the customer pays with a $10 bill, you need to check if you have a $5 bill to give back as change. If yes, decrease your $5 bill count and increase your $10 bill count. If no, you cannot provide the correct change, and it’s time to return false.
    3. If the customer pays with a $20 bill, you have two choices:
    • Preferably, if you have at least one $10 bill and one $5 bill, use them as they are larger denominations and help keep more smaller bills for potential future transactions. Decrease their respective counts.
    • If you don’t have a $10 bill, check if you have at least three $5 bills. If so, provide the change using these and update their count.
    • If neither condition is met, you can’t provide the required change, and again, you return false.

This stepped approach, relying simply on counting and deducting bills, ensures that each transaction is handled optimally with respect to the available change, ensuring sustainability of your lemonade sales throughout the day. The constraints indicate that there can be a significant number of transactions, thus, ensuring efficiency in your solution is critical.

Solutions

  • C++
  • Java
  • Python
cpp
class Solution {
public:
    bool handlePayments(vector<int>& payments) {
        int fives = 0;
        int tens = 0;

        for (int bill : payments) {
            switch(bill) {
                case 5:
                    fives++;
                    break;
                case 10:
                    if (fives == 0) return false;
                    fives--;
                    tens++;
                    break;
                case 20:
                    if (tens > 0 && fives > 0) {
                        tens--;
                        fives--;
                    } else if (fives >= 3) {
                        fives -= 3;
                    } else {
                        return false;
                    }
                    break;
            }
        }

        return true;
    }
};

The solution addresses the problem of determining whether you can provide the correct change for a series of transactions using bills of denominations 5, 10, and 20. The implementation is written in C++.

  • Initialize two integer counters, fives and tens, to keep track of the number of $5 and $10 bills available.
  • Iterate through each bill payment in the payments vector:
    • If the bill is $5, increase the count of fives.
    • If the bill is $10, check if there is at least one $5 bill available to give back as change. If so, decrement the fives count and increment the tens count. If a $5 bill is not available, return false.
    • For a $20 bill, the preferred change given back is one $10 bill and one $5 bill. If these are available, decrease both tens and fives by one each. If not, check if there are at least three $5 bills to give back as change. If neither condition is met, return false.
  • If you can provide the necessary change for all the transactions, return true.

This solution efficiently manages the bills' inventory and ensures correct change is available for each transaction.

java
class Solution {

    public boolean validateLemonadeChange(int[] customerBills) {
        int countFive = 0;
        int countTen = 0;

        for (int bill : customerBills) {
            switch (bill) {
                case 5:
                    countFive++;
                    break;
                case 10:
                    if (countFive > 0) {
                        countFive--;
                        countTen++;
                    } else {
                        return false;
                    }
                    break;
                case 20:
                    if (countTen > 0 && countFive > 0) {
                        countFive--;
                        countTen--;
                    } else if (countFive >= 3) {
                        countFive -= 3;
                    } else {
                        return false;
                    }
                    break;
            }
        }
        return true;
    }
}

The provided Java code offers a solution to the Lemonade Change problem. The core objective is to determine if it's possible to provide every customer with the correct change using the bills received from previous customers.

  • Initialize counters for $5 and $10 bills (countFive and countTen).
  • Iterate through the array of customer bills.
  • Use a switch-case structure to handle different bill types:
    • On receiving a $5 bill, increment countFive.
    • On receiving a $10 bill:
      • Check for at least one $5 bill for change. If available, decrement countFive and increment countTen. If not, return false.
    • On receiving a $20 bill:
      • Preferably, use one $10 and one $5 bill for change. Adjust counters appropriately. If not possible,
      • Alternatively, check for three $5 bills. If not sufficient, return false.
  • If all customers can be provided change successfully, return true.

This method ensures efficient tracking and usage of bills to accommodate all customer transactions. The return value specifies whether it's feasible to provide all customers with the necessary change based on the bills at hand.

python
class Solution:
    def processTransactions(self, payments: List[int]) -> bool:
        count_of_five = 0
        count_of_ten = 0
        
        for payment in payments:
            if payment == 5:
                count_of_five += 1
            elif payment == 10:
                if count_of_five == 0:
                    return False
                count_of_five -= 1
                count_of_ten += 1
            else:
                if count_of_ten > 0 and count_of_five > 0:
                    count_of_five -= 1
                    count_of_ten -= 1
                elif count_of_five >= 3:
                    count_of_five -= 3
                else:
                    return False
        return True

The Python program provided tackles the problem of managing lemonade change transactions during sales. The main objective of the function processTransactions is to determine whether you can provide every customer with correct change, given a sequence of bill payments.

Here’s how the solution works:

  • It starts by initializing two counters, count_of_five and count_of_ten, to track the number of $5 and $10 bills, respectively.

  • The program then processes each payment in the list:

    • If it receives a $5 bill, it simply increments the count_of_five.
    • If a $10 bill is provided, the code checks for the availability of a $5 bill to give back as change. If a $5 bill is unavailable, it returns False, indicating it is impossible to provide the correct change.
    • For a $20 payment, the preferred change is one $10 and one $5 bill. If that’s not possible, it tries to use three $5 bills. If neither option is viable, it returns False.
  • If the entire list of payments is processed without returning False, the function ends by returning True, indicating that it's possible to provide the correct change for all transactions.

The code effectively ensures that correct change handling is feasible, avoiding any scenario where a customer cannot be given the correct change. This implementation optimally utilizes available bills, prioritizing smaller denominations for higher flexibility in future transactions.

Comments

No comments yet.