Keep count of 5and10 bills you have:
1. Customer pays $5: take it (fives += 1)
2. Customer pays 10:giveone5 back if possible (fives -= 1, tens += 1)
3. Customer pays 20:prefergivingone10 + one 5;elsegivethree5s If you ever cannot give correct change, return false. No need to track 20billssinceyounevergivethemaschange.Time:O(n \log n).Space:O(1).ThetimecomplexityisO(n),notO(n log n)$, because you do not need to sort. You process bills in the order customers arrive.