← All coding problems
Lemonade Change
easyGreedy+10 XPTime limit 2s
Lemonade costs ₹5. Customers queue up and each pays with a single ₹5, ₹10 or ₹20 note. You start with no change and must give every customer correct change (₹0, ₹5 or ₹15) from the notes you've collected. Can you serve everyone, in order?
Input:
- line 1:
n - line 2: the
nnotes (each 5, 10 or 20)
Output: true or false.
Constraints: 1 ≤ n ≤ 10⁵.
Think about it: count your ₹5 and ₹10 notes. For ₹15 of change, prefer ₹10 + ₹5 over three ₹5s — the ₹5 notes are more useful later.
Sample input 1
5 5 5 5 10 20
Sample output 1
true
Sample input 2
5 5 5 10 10 20
Sample output 2
false
📖 Stuck? Learn the pattern
Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).