Determine whether you can provide correct change to every customer in order as they buy lemonade with $5, $10, or $20 bills.
You are running a lemonade stand where each lemonade costs $5. Customers arrive one by one in the given order. Each customer pays with a bill of value 5, 10, or 20.
For each customer, you must provide exact change immediately if needed:
You start with no money. Return whether it is possible to serve every customer in order without ever failing to make change.
bills, where each element is one of {5, 10, 20}.bills[i] is the bill paid by the i-th customer in arrival order.true if exact change can be given to every customer in order.false.bills[i] is one of 5, 10, or 20.Example 1
Input
bills = [5, 5, 5, 10, 20]
Output
true
Explanation
Collect three $5 bills, use one $5 bill as change for the $10 payment, then use one $10 and one $5 as change for the $20 payment.
Example 2
Input
bills = [5, 5, 10, 10, 20]
Output
false
Explanation
After serving the first four customers, you have two $5 bills and two $10 bills. The final $20 payment needs $15 change, but you do not have enough $5 bills to give change.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.