The "Lemonade Change" problem involves a line of customers at a lemonade stand. Each lemonade costs $5. Customers pay with either a $5, $10, or $20 bill, and you must provide correct change for each transaction. The challenge is to determine if you can provide change to every customer in the given order, starting with no money in your register.
bills
array by the bill they use to pay (5
, 10
, or 20
).true
if you can provide change to every customer, otherwise return false
.At first glance, you might think about simulating every possible way to give change for each transaction, but that would be inefficient. Instead, you can keep track of the number of $5 and $10 bills you have, since those are the only denominations you'll need to give change (you never give out $20 bills as change).
For each customer:
Here’s how we solve the problem step by step:
five
and ten
, both set to 0. These represent the number of $5 and $10 bills in your register.
bills
array:
five
by 1.five
by 1 and increment ten
by 1.false
.five
by 3.false
.false
, return true
.
This greedy approach ensures you always use higher denominations for change first, preserving smaller bills for future transactions.
Let's walk through an example input: bills = [5, 5, 5, 10, 20]
All customers receive correct change, so the answer is true
.
Brute-force approach: If you tried every possible combination of bills for change, the time complexity would be exponential and impractical for large inputs.
Optimized (greedy) approach:
The Lemonade Change problem is elegantly solved using a greedy strategy: always give out the highest denomination possible for change, and track only the number of $5 and $10 bills. This approach ensures efficient change-making and avoids unnecessary complexity. By focusing on the constraints and using simple counters, you can handle any sequence of customer payments in linear time with constant space.
class Solution:
def lemonadeChange(self, bills):
five, ten = 0, 0
for bill in bills:
if bill == 5:
five += 1
elif bill == 10:
if five == 0:
return False
five -= 1
ten += 1
else: # bill == 20
if ten > 0 and five > 0:
ten -= 1
five -= 1
elif five >= 3:
five -= 3
else:
return False
return True
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
int five = 0, ten = 0;
for (int bill : bills) {
if (bill == 5) {
five++;
} else if (bill == 10) {
if (five == 0) return false;
five--;
ten++;
} else { // bill == 20
if (ten > 0 && five > 0) {
ten--;
five--;
} else if (five >= 3) {
five -= 3;
} else {
return false;
}
}
}
return true;
}
};
class Solution {
public boolean lemonadeChange(int[] bills) {
int five = 0, ten = 0;
for (int bill : bills) {
if (bill == 5) {
five++;
} else if (bill == 10) {
if (five == 0) return false;
five--;
ten++;
} else { // bill == 20
if (ten > 0 && five > 0) {
ten--;
five--;
} else if (five >= 3) {
five -= 3;
} else {
return false;
}
}
}
return true;
}
}
var lemonadeChange = function(bills) {
let five = 0, ten = 0;
for (let bill of bills) {
if (bill === 5) {
five += 1;
} else if (bill === 10) {
if (five === 0) return false;
five -= 1;
ten += 1;
} else {
if (ten > 0 && five > 0) {
ten -= 1;
five -= 1;
} else if (five >= 3) {
five -= 3;
} else {
return false;
}
}
}
return true;
};