This reads to me like you are trying to partition a (multi)set of numbers (the "skill levels") into blocks of equal size ("teams") so that the averages ("total skill level") are as close to equal as possible.
To solve this, I would begin by computing the average skill level, which is the sum of the skill levels divided by the number of players, call this number s
. If there are to be m
teams total, each with k
players, giving a total of m*k
players, then the target skill level for each teams is k*s
.
Since your teams are already partially filled, the problem you have based on your example
I have 3 teams, They have 2 players, 3 players and 7 players. There is 18 players sitting on the sidelines waiting to be assigned.
is the following:
- Team A, with current skill level
a
, needs 8 players such thatp1 + ... + p8 + a = 10*s
- Team B, with current skill level
b
, needs 7 players such thatq1 + ... + q7 + b = 10*s
- Team C, with current skill level
c
, needs 3 players such thatr1 + r2 + r3 + c = 10*s
For a brute force solution, find the players for Team C first, then use the remaining players to solve for Teams A and B.
For a more clever solution, you need to realize that this is really a subset sum problem, and use one of the well-known algorithms for solving that. I recommend the dynamic programming solution as described in the linked article.