Arrays & Hashing

Lemonade

Estimated reading: 2 minutes 41 views

Problem: Lemonade Stand Change Problem

You are running a lemonade stand where each lemonade costs $5. Customers are standing in a queue to buy lemonade, and they will pay using either a $5, $10, or $20 bill. Your task is to determine whether you can provide the correct change to all customers in the order they appear, based on the following rules:

  • A customer paying with a $5 bill requires no change.
  • A customer paying with a $10 bill requires $5 in change.
  • A customer paying with a $20 bill requires $15 in change. You should give one $10 bill and one $5 bill, if possible. Otherwise, give three $5 bills.

You do not start with any change, and you must give the correct change to each customer in sequence.

Input:

  • bills: An integer array where each element represents the bill that the i-th customer pays with.
    • bills[i] will be one of 5, 10, or 20.
    • 0 <= bills.length <= 10,000

Output:

  • Return "pass" if you can provide the correct change for all customers.
  • Return "fail" if you cannot provide the correct change for any customer.

Examples

Input: [5, 5, 5, 10, 20]
Output: “pass”
Explanation:
– The first three customers pay with a $5 bill, so no change is needed.
– The fourth customer pays with a $10 bill and receives $5 in change.
– The fifth customer pays with a $20 bill and receives $15 in change ($10 + $5).

Input: [5, 5, 10, 10, 20]
Output: “fail”
Explanation:
– The first two customers pay with a $5 bill, so no change is needed.
– The third and fourth customers pay with a $10 bill and receive $5 in change each.
– The fifth customer pays with a $20 bill, but you cannot provide $15 in change because you only have two $10 bills.

				
					public static String lemonadeChange(int[] bills) {
        int five = 0, ten = 0; // Count of $5 and $10 bills

        for (int bill : bills) {
            if (bill == 5) {
                five++; // If customer pays with $5, no change is needed
            } else if (bill == 10) {
                if (five > 0) {
                    five--; // Give one $5 as change
                    ten++; // Collect one $10 bill
                } else {
                    return "fail"; // Cannot provide change
                }
            } else if (bill == 20) {
                if (ten > 0 && five > 0) {
                    ten--; // Give one $10
                    five--; // Give one $5
                } else if (five >= 3) {
                    five -= 3; // Give three $5 bills
                } else {
                    return "fail"; // Cannot provide change
                }
            }
        }

        return "pass"; // If we successfully served all customers
    }
				
			
				
					public static void main(String[] args) {
        // Test cases
        int[] bills1 = {5, 5, 5, 10, 20}; // Expected output: pass
        int[] bills2 = {5, 5, 10}; // Expected output: pass
        int[] bills3 = {10, 10}; // Expected output: fail
        int[] bills4 = {5, 5, 10, 10, 20}; // Expected output: fail

        System.out.println(lemonadeChange(bills1)); // pass
        System.out.println(lemonadeChange(bills2)); // pass
        System.out.println(lemonadeChange(bills3)); // fail
        System.out.println(lemonadeChange(bills4)); // fail
    }
				
			
Share this Doc

Lemonade

Or copy link

CONTENTS