A cooldown of k: remember the call each title last showed
Level 4 stretches the rule: a title shown in any of the last k calls is cooling and can't show. Level 3 is k = 1, and k = 0 turns the rule off.
Remembering only the last title isn't enough any more. Count the calls instead, and keep a dict from each title to the number of the call that last showed it:
- On call number
now, a title that last showed at callmis cooling whennow - m <= k. - A title that never showed isn't cooling.
Then get walks waiting from the front and shows the first key whose title isn't cooling. At most k titles can be cooling, so the walk skips at most k keys.
What if every title still waiting is cooling? That happens with a short catalog and a long cooldown. Show the one that showed longest ago, the smallest call number. That's the choice that keeps titles as far apart as the catalog allows.
Picking the best waiting title instead sounds simpler, and it backfires. Take three titles, A at 9, B at 5 and C at 1, with k = 3. One loop shows A, B, C, then C rises to 20. At call 4 all three are cooling. The best of them is C, which showed on call 3, the call just before. The longest ago is A:
| Fallback | Calls 1 to 6 |
|---|---|
| show the one shown longest ago | A, B, C, A, B, C |
| show the best | A, B, C, C, A, B |
With "the best", a longer cooldown produces the very repeat that k = 1 prevents.
The editor holds cooling and pick. Write cooling(title, last_call, now, k) and pick(waiting, last_call, now, k), with the fallback to the title shown longest ago.