15% off all plans & roadmaps Use code
See plansProblem editorial
You are given bills, where each element is the bill paid by one customer, in arrival order. Each bill is 5, 10, or 20 dollars, and one lemonade costs exactly 5 dollars. The seller starts with no bills.
Lemonade Change is a Greedy Patterns problem, in the Sorting Based subpattern. The pattern covers interval scheduling, jump games, stock trading, and task scheduling
The reference solution runs in O(n), where n is the number of bills. time and O(1) space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Lemonade Change is a Greedy Patterns problem, in the Sorting Based subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.