Experiment Buckets I
Pinterest's experiment framework hashes every user into one of n buckets, 0..n-1. Each bucket is assigned to at most one experiment group; unassigned buckets are null. You are given the current assignment and new target sizes:
current = ["A", "A", "A", "B", null, null] // per-bucket group
targets = { "A": 1, "B": 3 } // desired bucket count per groupProduce a new assignment meeting the targets exactly while changing as few buckets as possible — every changed bucket disrupts the experiment for the users hashed into it. Groups present in current but absent from targets lose all their buckets; a bucket going from a group to null counts as a change. You may assume the targets fit: the target counts sum to at most n.
The judge accepts any assignment that meets the targets exactly with the minimum possible number of changed buckets.
Hints
Hint 1
A group can keep at most min(currentCount, target) of its buckets, so keep each group's buckets up to its target in a first pass; every other assigned bucket is surplus and must change no matter what.
Hint 2
Fill deficits from surplus buckets before touching never-assigned ones: a surplus bucket's change is already paid for, while an untouched null bucket only costs a change if you use it.