890. Lemonade Change
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to simulate transactions at a lemonade stand. Each lemonade costs $5. Customers pay with $5, $10, or $20 bills. We start with no money and must provide the correct change for each customer in the order they appear. The goal is to determine if we can successfully complete all transactions.
Solution
Lemonade Change in Python
class Solution:
def lemonadeChange(self, bills: List[int]) -> bool:
five_count = 0
ten_count = 0
# We don't need to track $20 bills since they can't be used for change.
for bill in bills:
if bill == 5:
# Customer pays with $5. We receive it.
five_count += 1
elif bill == 10:
# Customer pays with $10. We need to give $5 change.
if five_count > 0:
five_count -= 1
ten_count += 1
else:
# No $5 bill to give as change.
return False
else: # bill is 20
# Customer pays with $20. We need to give $15 change.
# Greedy approach: First, try to use one $10 and one $5 bill.
# This is better because $5 bills are more versatile.
if ten_count > 0 and five_count > 0:
ten_count -= 1
five_count -= 1
# If that's not possible, try using three $5 bills.
elif five_count >= 3:
five_count -= 3
else:
# Not enough change.
return False
# If we successfully processed all customers, return true.
return True
Lemonade Change in Java
class Solution {
public boolean lemonadeChange(int[] bills) {
int five = 0, ten = 0;
// We don't need to track twenty-dollar bills since they can't be used for change.
for (int bill : bills) {
if (bill == 5) {
// Customer pays with $5. We receive it.
five++;
} else if (bill == 10) {
// Customer pays with $10. We need to give $5 change.
if (five == 0) {
// No $5 bill to give as change.
return false;
}
five--;
ten++;
} else { // bill is 20
// Customer pays with $20. We need to give $15 change.
// Greedy approach: First, try to use one $10 and one $5 bill.
// This is better because $5 bills are more versatile.
if (ten > 0 && five > 0) {
ten--;
five--;
} else if (five >= 3) {
// If we can't use a $10 bill, try to use three $5 bills.
five -= 3;
} else {
// Not enough change.
return false;
}
}
}
// If we successfully processed all customers, return true.
return true;
}
}
Lemonade Change in C++
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
int five = 0, ten = 0;
// We don't need to track twenty-dollar bills since they can't be used for change.
for (int bill : bills) {
if (bill == 5) {
// Customer pays with $5. We receive it.
five++;
} else if (bill == 10) {
// Customer pays with $10. We need to give $5 change.
if (five > 0) {
five--;
ten++;
} else {
// No $5 bill to give as change.
return false;
}
} else { // bill is 20
// Customer pays with $20. We need to give $15 change.
// Greedy approach: First, try to use one $10 and one $5 bill.
// This is better because $5 bills are more versatile.
if (ten > 0 && five > 0) {
ten--;
five--;
} else if (five >= 3) {
// If we can't use a $10 bill, try to use three $5 bills.
five -= 3;
} else {
// Not enough change.
return false;
}
}
}
// If we successfully processed all customers, return true.
return true;
}
};
Lemonade Change in TypeScript
function lemonadeChange(bills: number[]): boolean {
let fiveCount = 0;
let tenCount = 0;
// We don't need to track twenty-dollar bills since they can't be used for change.
for (const bill of bills) {
if (bill === 5) {
// Customer pays with $5. We receive it.
fiveCount++;
} else if (bill === 10) {
// Customer pays with $10. We need to give $5 change.
if (fiveCount > 0) {
fiveCount--;
tenCount++;
} else {
// No $5 bill to give as change.
return false;
}
} else { // bill is 20
// Customer pays with $20. We need to give $15 change.
// Greedy approach: First, try to use one $10 and one $5 bill.
// This is better because $5 bills are more versatile.
if (tenCount > 0 && fiveCount > 0) {
tenCount--;
fiveCount--;
} else if (fiveCount >= 3) {
// If we can't use a $10 bill, try to use three $5 bills.
fiveCount -= 3;
} else {
// Not enough change.
return false;
}
}
}
// If we successfully processed all customers, return true.
return true;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.