Keep count of 10 bills you have:
Customer pays $5: take it (fives += 1)
Customer pays 5 back if possible (fives -= 1, tens += 1)
Customer pays 10 + one 5s If you ever cannot give correct change, return false. No need to track O(n \log n)O(1)O(n)O(n log n)$, because you do not need to sort. You process bills in the order customers arrive.