Lemonade Change

You are given an array bills representing the bills each customer pays in order at a lemonade stand. Each lemonade costs $5, and customers pay with a $5, $10, or $20 bill. Return true if you can provide correct change to every customer in order, otherwise return false. Pattern focus: Greedy Choice Property. Always keep as many $5 bills as possible and use a $10 bill only when needed.

Input Format

bills = sequence of customer payments

Output Format

true if all customers can be served, otherwise false

Constraints

  • 1 <= bills.length <= 10^5
  • bills[i] is one of 5, 10, or 20

Examples

Example 1:

Input:

bills = [5,5,5,10,20]

Output:

true

Explanation:

Every customer can be served; the $20 bill is changed using one $10 and one $5.

Example 2:

Input:

bills = [5,5,10,10,20]

Output:

false

Explanation:

The last customer needs $15 change, but only $10 bills remain.

Example 3:

Input:

bills = [5,5,5,5,20,20,20]

Output:

false

Explanation:

There is not enough $10 and $5 change to serve the later $20 customers.

Loading...
Lemonade Change - Greedy DSA Problem