dsa · easy

Lemonade Change

At a lemonade stand each cup costs 5. Customers line up and pay with a 5, 10, or 20 bill, one at a time. You start with no bills and must give every customer exact change from the bills you have collected so far. Return true if you can serve every customer in order, false otherwise.

Arguments

Example

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

Three 5s arrive (no change needed), then a 10 (give one 5), then a 20 (give one 5 and one 10). Every customer is served → true.

bills = [5,5,10,10,20] → after the two 10s you hold no 5s, so the 20 cannot be given change → false.

Constraints

1 <= bills.length <= 10^5 bills[i] is either 5, 10, or 20 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.

Examples

Example 1

Input:
[5,5,5,10,20]

Expected:
true

Example 2

Input:
[5,5,10,10,20]

Expected:
false

Example 3

Input:
[5,5,5,10,5,5,10,20,20,20]

Expected:
false

Open in the Dojo editor