Each lemonade at a booth sells for $5. Consumers are lining up to place individual orders, following the billing order. Every consumer will purchase a single lemonade and may pay with a $5, $10, or $20 bill. Each customer must receive the appropriate change so that the net transaction is $5. Initially, there is no change available.
Determine if it is possible to provide the correct change to every customer. Return true if the correct change can be given to every customer, and false otherwise.
Given an integer array bills, where bills[i] is the bill the ith customer pays, return true if the correct change can be given to every customer, and false otherwise.
Input : bills = [5, 5, 10, 5, 20]
Output : true
Explanation : Initially we have $0 available for change.
From first two customers, we will collect two $5 bills in order. After the first two customers we have two $5 bills available with us for change.
From the third customer , we collect bill of $10 and give back $5. After third customer we have one $5 and one $10 bill available with us for change.
From fourth customer , we collect $5 bill. After fourth customer we have two $5 and one $10 bills available with us for change if required.
From fifth customer , we collect bill of $20 and give back $15 (one $10 + one $5 bill).
Since all the customers did receive the change correctly , so we return true.
Input : bills = [5, 5, 10, 10, 20]
Output : false
Explanation : From first two customers, we will collect two $5 bills in order. After the first two customers we have two $5 bills available with us for change.
From third customer , we collect $10 and give back $5. After the third customer we have one $5 and one $10 bill available with us for change.
From fourth customer , we collect $10 and give back $5. After the fourth customer we have two $10 bill available with us for change.
From fifth customer , we collect $20 , we cannot give the $15 change as we have two $10 bills.
Since all the customers did not receive the change correctly , the we return false.
Input : bills = [5, 5, 10, 20]
When all the customers can be served
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
/* Function to find whether each customer can
be provided with correct change */
bool lemonadeChange(vector<int>& bills) {
// Counter for $5
int five = 0;
// Counter for $10
int ten = 0;
// Iterate through each customer's bill
for (int i = 0; i < bills.size(); i++) {
/* If the customer's
bill is $5 */
if (bills[i] == 5) {
// Increment $5
five++;
}
/* If the customer's
bill is $10 */
else if (bills[i] == 10) {
/* Check if there are $5
bills available to give change */
if (five) {
// Use one $5
five--;
// Receive one $10
ten++;
}
/* If no $5 bill
available, return false */
else return false;
}
/* If the customer's
bill is $20 */
else {
/* Check if there are both
$5 and $10 bills
available to give change */
if (five && ten) {
// Use one $5
five--;
// Use one $10
ten--;
}
/* If there are not enough $10 bills,
check if there are at least
three $5 bills available */
else if (five >= 3) {
// Use three $5 bills
five -= 3;
}
/* If unable to give
change, return false */
else return false;
}
}
// Return true
return true;
}
};
int main() {
vector<int> bills = {5, 5, 5, 10, 20};
cout << "Queues of customers: ";
for (int bill : bills) {
cout << bill << " ";
}
cout << endl;
Solution solution;
bool ans = solution.lemonadeChange(bills);
if (ans)
cout << "It is possible to provide change for all customers." << endl;
else
cout << "It is not possible to provide change for all customers." << endl;
return 0;
}
import java.util.*;
class Solution {
/* Function to find whether each customer can
be provided with correct change */
public boolean lemonadeChange(int[] bills) {
// Counter for $5
int five = 0;
// Counter for $10
int ten = 0;
// Iterate through each customer's bill
for (int bill : bills) {
/* If the customer's
bill is $5 */
if (bill == 5) {
// Increment $5
five++;
}
/* If the customer's
bill is $10 */
else if (bill == 10) {
/* Check if there are $5
bills available to give change */
if (five > 0) {
// Use one $5
five--;
// Receive one $10
ten++;
} else {
// If no $5 bill available, return false
return false;
}
}
/* If the customer's
bill is $20 */
else {
/* Check if there are both
$5 and $10 bills
available to give change */
if (five > 0 && ten > 0) {
// Use one $5
five--;
// Use one $10
ten--;
}
/* If there are not enough $10 bills,
check if there are at least
three $5 bills available */
else if (five >= 3) {
// Use three $5 bills
five -= 3;
}
/* If unable to give
change, return false */
else {
return false;
}
}
}
// Return true
return true;
}
public static void main(String[] args) {
int[] bills = {5, 5, 5, 10, 20};
System.out.print("Queues of customers: ");
for (int bill : bills) {
System.out.print(bill + " ");
}
System.out.println();
Solution solution = new Solution();
boolean ans = solution.lemonadeChange(bills);
if (ans)
System.out.println("It is possible to provide change for all customers.");
else
System.out.println("It is not possible to provide change for all customers.");
}
}
class Solution:
""" Function to find whether each customer can
be provided with correct change """
def lemonadeChange(self, bills):
# Counter for $5
five = 0
# Counter for $10
ten = 0
# Iterate through each customer's bill
for bill in bills:
# If the customer's bill is $5
if bill == 5:
# Increment $5
five += 1
# If the customer's bill is $10
elif bill == 10:
# Check if there are $5 bills available to give change
if five > 0:
# Use one $5
five -= 1
# Receive one $10
ten += 1
else:
# If no $5 bill available, return false
return False
# If the customer's bill is $20
else:
# Check if there are both $5 and $10 bills available to give change
if five > 0 and ten > 0:
# Use one $5
five -= 1
# Use one $10
ten -= 1
# If there are not enough $10 bills,
# check if there are at least three $5 bills available
elif five >= 3:
# Use three $5 bills
five -= 3
else:
# If unable to give change, return false
return False
# Return true
return True
# Example usage
if __name__ == "__main__":
bills = [5, 5, 5, 10, 20]
print("Queues of customers: ", end="")
for bill in bills:
print(bill, end=" ")
print()
solution = Solution()
ans = solution.lemonadeChange(bills)
if ans:
print("It is possible to provide change for all customers.")
else:
print("It is not possible to provide change for all customers.")
class Solution {
/* Function to find whether each customer can
be provided with correct change */
lemonadeChange(bills) {
// Counter for $5
let five = 0;
// Counter for $10
let ten = 0;
// Iterate through each customer's bill
for (let bill of bills) {
/* If the customer's
bill is $5 */
if (bill === 5) {
// Increment $5
five++;
}
/* If the customer's
bill is $10 */
else if (bill === 10) {
/* Check if there are $5
bills available to give change */
if (five > 0) {
// Use one $5
five--;
// Receive one $10
ten++;
}
/* If no $5 bill available, return false */
else {
return false;
}
}
/* If the customer's
bill is $20 */
else {
/* Check if there are both
$5 and $10 bills
available to give change */
if (five > 0 && ten > 0) {
// Use one $5
five--;
// Use one $10
ten--;
}
/* If there are not enough $10 bills,
check if there are at least
three $5 bills available */
else if (five >= 3) {
// Use three $5 bills
five -= 3;
}
/* If unable to give change, return false */
else {
return false;
}
}
}
// Return true
return true;
}
}
// Example usage
const bills = [5, 5, 5, 10, 20];
console.log("Queues of customers: " + bills.join(" "));
const solution = new Solution();
const ans = solution.lemonadeChange(bills);
if (ans)
console.log("It is possible to provide change for all customers.");
else
console.log("It is not possible to provide change for all customers.");
Q: What if there are no $5 bills in the array?
A: If there are no $5 bills and a customer pays with a $10 or $20 bill, return false immediately since change cannot be provided.
Q: What if there are multiple $20 bills in a row?
A: The algorithm handles each transaction independently. If enough $10 and $5 bills are available for each $20 transaction, it proceeds. Otherwise, it terminates when change can't be given.
Q: What happens if customers are allowed to pay with other denominations (e.g., $2, $50)?
A: Extend the tracking system to handle these denominations and implement rules to prioritize their usage for making change.
Q: How would you modify the solution if the booth starts with some initial change?
A: Add the initial counts of $5 and $10 bills to the respective variables at the beginning of the simulation.