Lemonade Change

At a lemonade stand each lemonade costs $5. Customers stand in a queue and pay one at a time, in the order given by bills, each with a single $5, $10 or $20 bill. You must give every customer correct change so that each pays exactly $5, and you start with no change at all. Return true if you can give every customer correct change, otherwise false.

30:00

Lemonade Change
easy
Free written solution
Topics
Companies

At a lemonade stand each lemonade costs 5.Customersstandinaqueueandpayoneatatime,intheordergivenbybills,eachwithasingle5. Customers stand in a queue and pay one at a time, in the order given by bills, each with a single 5, 10or10 or 20 bill. You must give every customer correct change so that each pays exactly $5, and you start with no change at all. Return true if you can give every customer correct change, otherwise false.

Example 1:
Input: {"bills":[5,5,5,10,20]}
Output: true
Constraints:
  • 1≤bills.length≤1051 \leq \text{bills.length} \leq 10^5

  • bills[i]\text{bills}[i] is either 55, 1010, or 2020.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"bills":[5,5,5,10,20]}

5 customers in line and an empty drawer. Only $5 and $10 bills can ever be handed back as change.

Customers in line

$5

#0

$5

#1

$5

#2

$10

#3

$20

#4

counter

Cash drawer (only $5 and $10 matter)

$5 slot

empty

0

$10 slot

empty

0

$20 bills are never given back, so they are not tracked.

Variables
VariableValue
five0
ten0
DepthFunction Call
Stack empty
0/11