Advertisement

Lemonade Change - LeetCode 860 Solution

Lemonade Change - Complete Solution Guide

Lemonade Change is LeetCode problem 860, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 i th customer pays, return true

Detailed Explanation

The problem simulates a lemonade stand where each lemonade costs $5. Customers pay with $5, $10, or $20 bills. The goal is to determine if you can always provide the correct change to each customer, given the order of their payments. You start with no change in hand. If at any point you cannot give the correct change, you return `false`; otherwise, you return `true`.

Solution Approach

The provided code uses a greedy approach. It iterates through the `bills` array and keeps track of the number of $5 and $10 bills you have. For each customer, it checks if you have sufficient change to give back. If a customer pays with a $5 bill, you increment the count of $5 bills. If they pay with a $10 bill, you check if you have a $5 bill to give as change. If they pay with a $20 bill, you first try to use a $10 bill and a $5 bill. If that's not possible, you check if you have at least three $5 bills. If neither condition is met, you cannot provide the correct change and return `false`. If you can serve all customers, you return `true`.

Step-by-Step Algorithm

  1. Step 1: Initialize variables `five` and `ten` to 0 to represent the count of $5 and $10 bills, respectively.
  2. Step 2: Iterate through the `bills` array.
  3. Step 3: If the current bill is $5, increment `five`.
  4. Step 4: If the current bill is $10, check if `five > 0`. If so, decrement `five` and increment `ten`. Otherwise, return `false`.
  5. Step 5: If the current bill is $20, first check if both `ten > 0` and `five > 0`. If so, decrement both `ten` and `five`. Otherwise, check if `five >= 3`. If so, decrement `five` by 3. Otherwise, return `false`.
  6. Step 6: After iterating through all the bills, if no `false` has been returned, return `true`.

Key Insights

  • Insight 1: The order of bills matters significantly. You must process them in the order given.
  • Insight 2: You only need to keep track of the number of $5 and $10 bills you have, as you can always use them to make change.
  • Insight 3: Prioritize using a $10 bill and a $5 bill to give change before using three $5 bills, as this maximizes your chances of having enough $5 bills for future transactions.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Greedy.

Companies

Asked at: Zalando.