{x}
blog image

Lemonade Change

At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you and order one at a time (in the order specified by bills). Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill. You must provide the correct change to each customer so that the net transaction is that the customer pays $5.

Note that you do not have any change in hand at first.

Given an integer array bills where bills[i] is the bill the ith customer pays, return true if you can provide every customer with the correct change, or false otherwise.

 

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.

Solution Explanation: Lemonade Change

The problem asks whether a lemonade stand can provide change to every customer, given that each lemonade costs $5 and customers pay with $5, $10, or $20 bills. The stand starts with no change.

The optimal approach is a greedy algorithm. We keep track of the number of $5 and $10 bills we have. For each customer's payment:

  • $5 bill: Increment the count of $5 bills.
  • $10 bill: If we have at least one $5 bill, give it as change and decrement the $5 bill count and increment the $10 bill count. Otherwise, we can't give change.
  • $20 bill: We try to give change using one $10 and one $5. If we have at least one $10 and one $5, use them and decrement both counts. Otherwise, we use three $5 bills. If we don't have enough $5 bills, we can't give change.

If at any point we have a negative number of $5 bills, it means we can't provide the correct change, and we return false. Otherwise, we return true.

Time and Space Complexity Analysis

  • Time Complexity: O(N), where N is the number of customers (the length of the bills array). We iterate through the array once.
  • Space Complexity: O(1). We only use a constant amount of extra space to store the counts of $5 and $10 bills.

Code Explanation (Python3 - Solution 1 as an example)

class Solution:
    def lemonadeChange(self, bills: List[int]) -> bool:
        five = ten = 0  # Initialize counts of $5 and $10 bills
        for v in bills:
            if v == 5:
                five += 1  # Customer pays with $5
            elif v == 10:
                if five > 0:  # Give change using a $5 bill
                    five -= 1
                    ten += 1
                else:
                    return False  # Not enough $5 bills
            else:  # Customer pays with $20
                if ten > 0 and five > 0:  # Give change using $10 and $5
                    ten -= 1
                    five -= 1
                elif five >= 3:  # Give change using three $5 bills
                    five -= 3
                else:
                    return False  # Not enough change
 
            # Check for negative $5 bills
            if five < 0:
                return False
 
        return True  # All customers received correct change
 

The other language solutions follow the same logic, adapting the syntax to their respective languages. The one-liner solutions cleverly use bitwise XOR (^) and short-circuiting to condense the logic, but they achieve the same underlying greedy approach.