A worked example: spread out repeated letters
A classic interview problem uses the same cooldown with a heap: arrange letters so that no letter comes back within k places. Given {"a": 3, "b": 2, "c": 1} and k = 1, one answer is ababac.
The plan is greedy. At each place, take the letter with the most copies left, because the most common letter is the hardest to fit. A letter you just used can't come back for k places, so it waits in a queue until its return place.
heap: letters ready to use, as(-copies_left, letter), so the most copies pops first and ties go alphabetically.cooling: letters waiting, as(return_place, -copies_left, letter).
Each place does three things: move back every letter whose return place has come, pop the best letter from the heap, and if it has copies left, put it in cooling until place + k + 1. Places come in order and every return place is the current place plus the same k + 1, so the queue is already sorted by return place. A plain list or a deque works, with no second heap.
If the heap is empty at some place, every letter left is cooling, and nothing can go there. Then no arrangement exists, and the answer is an empty string. {"a": 3, "b": 1} with k = 2 fails that way: after a and b, only a is left, and it showed one place ago.
Here's the billboard's cooldown with one difference: a billboard can always show something, using the fallback, and this problem can't, so it gives up.
The editor holds spread with its heap and queue set up. Write the body of the loop in spread(counts, k).