Gossip build propagation

Write codeEasy15 min

Your task

A release tool ships a new build across several clusters by gossip. In each cluster one seed machine holds the build before the first round. During a round, every machine that already has the build sends it to fanout machines that do not have it yet, and the senders coordinate so no machine is sent the same build twice. A machine that receives the build in a round can forward it in the next one. cluster_sizes[i] is the number of machines in cluster i. Return a list holding, for each cluster in input order, the number of rounds until every machine in that cluster has the build. A cluster of one machine, or of none, needs no rounds. When fanout is 0 nothing ever spreads, so report -1 for any cluster of two or more machines.

Examples

rollout_rounds_needed(cluster_sizes: list[int], fanout: int) -> list[int]
Example 1
Inputrollout_rounds_needed([1, 2, 3, 10], 1)
Output[0, 1, 2, 4]
Example 2
Inputrollout_rounds_needed([9, 10, 27, 28], 2)
Output[2, 3, 3, 4]
Example 3
Inputrollout_rounds_needed([5, 1, 0], 0)
Output[-1, 0, 0]