Minimum Generations to Equalize Neural Network Layer Counts

viaGlassdoor

Problem: A neural network has n layers with layer[i] neurons in the i-th layer. In each generation, at most one layer can have neurons added: odd generations add 1 neuron to at most one layer, even generations add 2 neurons to at most one layer. Find the minimum number of generations needed to make all layers have an equal neuron count.

Constraints: n can be up to 10^5; neuron counts can vary widely.

Example: n=4, layers=[1,1,2,4] -> minimum generations = 6 (as shown by simulating additions to layers 1 and 2 across generations 1-6 until all reach 4).

Approach: Binary search on the number of generations g: for a candidate g, compute how many 'odd' slots (ceil(g/2)) and 'even' slots (floor(g/2), each worth 2) are available, and greedily check whether the total deficit (sum of target - layer[i] for all layers, where target = max(layers)) can be covered using at most one odd-add and one even-add per generation across all layers - since only one layer can be boosted per generation, the deficits must each be achievable within the allotted odd/even additions assigned across generations. Binary search over g and validate feasibility to find the minimum g.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account