Ferry Fare Change
You run the ticket window for a river ferry. A crossing costs $5, and each passenger pays with one $5, $10 or $20 bill. Your cash box starts empty, so the only change you can give is bills earlier passengers paid with. You know today's queue in advance and want to find out whether everyone can be served.
Your task
Write canGiveChange(bills). bills[i] is the bill the i-th passenger pays with, in queue order. Each passenger buys exactly one $5 ticket. Return true if you can give every passenger correct change, and false as soon as one passenger cannot get it.
Bills you receive go into the cash box and can be used as change for later passengers. An empty queue returns true.
canGiveChange(bills: int[]) → bool
Examples
bills = [5, 5, 10, 20]
true
After two exact payments the box holds two $5. The $10 passenger gets a $5 back, leaving one $5 and one $10. The $20 passenger needs $15, so you give back the $10 and the $5.
bills = [5, 10, 10]
false
After two passengers the box holds one $10 and no $5. The third passenger needs $5 back, and a $10 bill cannot be split.
Constraints
- 0 ≤ bills.length ≤ 100,000
- bills[i] is 5, 10 or 20
Start with brute force
Each $20 passenger can get change up to two ways. Trying both options for each of them means up to 2k sequences for k such passengers, which is about a trillion for 40. Most of that work is wasted, because one choice is never worse than the other.
Hints
0 of 4 shown- Hint 1
A $5 and a $10 are not equally useful as change. Think about which passengers a $10 can help and which ones need a $5.
- Hint 2
A $10 passenger needs $5 back. A $20 passenger needs $15, which you can give as $10 + $5 or as $5 + $5 + $5. The $10 is only useful in the first combination.
- Hint 3
When a $20 arrives and either combination works, consider which choice leaves more $5 bills for later passengers.
- Hint 4
Count the $5 and $10 bills in the box, since a $20 is never useful as change. For a $10 payment, give back a $5. For a $20 payment, prefer $10 + $5 and use three $5s only when there is no $10. Return false as soon as a payment cannot be handled.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
A $5 can help any future passenger, while a $10 only helps a $20 passenger. When you have a choice, spend the less flexible bill and keep the $5s.
The trace below uses [5, 5, 5, 5, 10, 20, 10] and tracks the counts as (fives, tens).
- The four $5 passengers leave the box at (4, 0).
- The $10 passenger gets a $5 back, giving (3, 1).
- The $20 passenger gets a $10 and a $5 back, not three $5s, giving (2, 0).
- The last $10 passenger gets a $5 back, giving (1, 1). Everyone has been served, so the answer is
true.
Paying the $20 with three $5s would leave (0, 1), and the last passenger could not get their $5 back. The total amount of money would be the same, but the bills would be the wrong ones.
function canGiveChange(bills) {
let fives = 0, tens = 0; // $20 bills are never given as change
for (const bill of bills) {
if (bill === 5) {
fives++;
} else if (bill === 10) {
if (fives === 0) return false;
fives--; tens++;
} else if (tens > 0 && fives > 0) {
tens--; fives--; // spend the less useful $10 first
} else if (fives >= 3) {
fives -= 3;
} else {
return false;
}
}
return true;
}def can_give_change(bills):
fives = tens = 0 # $20 bills are never given as change
for bill in bills:
if bill == 5:
fives += 1
elif bill == 10:
if fives == 0:
return False
fives -= 1
tens += 1
elif tens > 0 and fives > 0:
tens -= 1 # spend the less useful $10 first
fives -= 1
elif fives >= 3:
fives -= 3
else:
return False
return TrueRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.