#860

Lemonade Change

Easy Greedy change-making

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Lemonade costs $5. Customers pay with $5, $10 or $20 bills in order, and you start with no change. Decide whether you can give everyone correct change.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: can you give correct change to every customer, in order?

Why this works: for a $20, prefer $10 + $5 over three $5s; $5 bills are more useful later.

Example: [5, 5, 10, 10, 20]

5, 5 → two fives10 → give 5 (one five left)10 → give 5 (no fives)20 → no change possible → false

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Paying a $20 with three $5s first, which runs out of $5s too early.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(1).
Topic: Greedy
🎬 Episodes 69–73: video coming soon