Spread Pods Across Zones for Anti-Affinity
Problem Statement
To satisfy topology spread constraints, place pods across zones so the difference between the most-loaded and least-loaded zone stays minimal. Report the max skew after greedily placing each pod in the currently-least-loaded zone.
Background
Given zones (each starting with some existing pod count) and a number of new pods to place, assign each new pod one at a time to the zone with the fewest pods (ties broken by zone index). After placing all, the skew is max(counts) - min(counts).
Your Task
def max_skew(initial, new_pods):
- initial: list of ints, current pod count per zone.
- new_pods: int, pods to place.
- Return the final skew (int).
Input Format
- initial (list of ints), new_pods (int).
Output Format
- A single int.
Sample
print(max_skew([0, 0, 2], 3))
Output:
1
Example
print(max_skew([0, 0, 2], 3))
1
- Start with the initial zone counts and identify the zone with the fewest pods to place the first of the 3 new pods. Zones 0 and 1 are tied at 0, so the pod goes to Zone 0 (lowest index), updating counts to .
- Place the second pod in the currently least-loaded zone. Zone 1 has 0 pods, which is less than Zone 0 (1) and Zone 2 (2), so the pod goes to Zone 1, updating counts to .
- Place the third pod in the least-loaded zone. Zones 0 and 1 are tied at 1 pod, so the pod goes to Zone 0 (lowest index), updating counts to .
- Calculate the final skew by finding the difference between the maximum and minimum pod counts in the final state : .
- The final output is 1
Constraints
- Place each pod into the least-loaded zone (ties: smaller index).
- Skew = max(counts) - min(counts) after all placements.
initialis non-empty.