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 <= 105bills[i] is either 5, 10, or 20.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are running a lemonade stand where every drink costs the same amount, and you need to figure out how to give correct change to every customer in line. The brute force approach involves looking ahead at all possible combinations of change-giving strategies to see if you can successfully get through the entire line of customers without ever running out of the right bills.
Here's how the algorithm would work step-by-step:
class LemonadeStandSolver:
def lemonadeChange(self, customer_bills: list[int]) -> bool:
def explore_change_combinations(customer_index: int, five_dollar_bills: int, ten_dollar_bills: int) -> bool:
if customer_index == len(customer_bills):
return True
current_payment = customer_bills[customer_index]
# We branch through all possible bill combinations to ensure every future path is fully explored
if current_payment == 5:
return explore_change_combinations(customer_index + 1, five_dollar_bills + 1, ten_dollar_bills)
elif current_payment == 10:
if five_dollar_bills >= 1:
if explore_change_combinations(customer_index + 1, five_dollar_bills - 1, ten_dollar_bills + 1):
return True
return False
elif current_payment == 20:
# Backtracking requires checking a 10 and a 5 first before trying three 5 bills for change
if ten_dollar_bills >= 1 and five_dollar_bills >= 1:
if explore_change_combinations(customer_index + 1, five_dollar_bills - 1, ten_dollar_bills - 1):
return True
if five_dollar_bills >= 3:
if explore_change_combinations(customer_index + 1, five_dollar_bills - 3, ten_dollar_bills):
return True
return False
return False
return explore_change_combinations(0, 0, 0)The optimal strategy is a greedy approach that processes customers one by one while keeping a running count of the smaller bills available in the cash register. Whenever change is needed, it always prioritizes handing back the largest possible bill first, which preserves the smaller bills for future customers who might need exact change.
Here's how the algorithm would work step-by-step:
class Solution:
def lemonadeChange(self, customer_bills: list[int]) -> bool:
count_five_dollar_bills = 0
count_ten_dollar_bills = 0
for current_bill in customer_bills:
if current_bill == 5:
count_five_dollar_bills += 1
elif current_bill == 10:
# A ten-dollar payment requires returning a five-dollar bill as change.
if count_five_dollar_bills > 0:
count_five_dollar_bills -= 1
count_ten_dollar_bills += 1
else:
return False
else:
# Prioritize using a ten and a five to preserve smaller bills.
if count_ten_dollar_bills > 0 and count_five_dollar_bills > 0:
count_ten_dollar_bills -= 1
count_five_dollar_bills -= 1
elif count_five_dollar_bills >= 3:
count_five_dollar_bills -= 3
else:
return False
return True| Case | How to Handle |
|---|---|
| Empty bills array | Return true immediately since no transactions need to be processed. |
| Single customer paying with a 5 dollar bill | No change is needed, so we increment our 5 dollar count and return true. |
| Single customer paying with a 10 or 22 dollar bill | Return false instantly because we do not have any 5 dollar bills in hand to provide change. |
| Customer pays with 22 dollars when we only have 5 dollar bills | Greedily use one 10 dollar bill and two 5 dollar bills if available, or three 5 dollar bills otherwise. |
| Customer pays with 10 dollars when we have zero 5 dollar bills | Return false because we cannot make change for the 10 dollar bill without a 5 dollar bill. |
| Customer pays with 22 dollars and we have both 10 and 5 dollar bills available | Greedily give one 10 and one 5 dollar bill rather than three 5s to preserve smaller bills for future 10 dollar changes. |
| Extremely large queue of customers up to the maximum constraints | Process the array in a single linear scan with O(1) space complexity to ensure optimal performance without memory limits. |
| Invalid bill denominations not equal to 5, 10, or 22 | Treat unrecognized bills as incapable of being accepted and immediately return false. |