Greedy Algorithms · Coding problem

Lemonade Change

A free coding problem from the Greedy Denomination Coins skill. Read it here, then solve it in the app in Java or Python.

  • Easy
  • Coding problem
  • Free
  • Java and Python

The problem

A lemonade stand charges $5 a cup. Customers come one at a time, each paying with a single $5, $10 or $20 bill. The stand starts with no cash and has to hand back exact change from the bills it has collected so far. Return whether every customer gets their change.

Input format

n
<n bills>

Example

Input:
6
5 5 10 5 20 10
Output: true

The $20 gets a $10 and a $5 back, and the last $10 gets the remaining $5. With 5 10 10, the third customer can't be paid back: the only $5 already went to the second.

Constraints

  • 1 <= n <= 10^5
  • Every bill is 5, 10 or 20

How it's graded

  • Run checks your code against the examples. Submit also runs a hidden suite in four groups - basic cases, edge cases, random inputs and performance tests at the size limit - and shows which groups pass, never the hidden inputs.
  • Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
  • Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.

More problems in Greedy Denomination Coins

Solve it Lemonade Change free

No card needed. Lessons, practice questions and easy problems are free forever.

Free plan, no card · 7-day Pro trial · Cancel anytime