Loading content...
You are organizing an event with n participants, where each participant is uniquely identified by an index from 0 to n - 1. Each participant has specified the exact team size they wish to be part of—this information is provided in an integer array called teamCapacity, where teamCapacity[i] represents the desired team size for participant i.
Your task is to form teams such that every participant is placed in exactly one team, and the size of each team matches the preference stated by all members within that team. In other words, if a participant specifies a team capacity of k, they must be placed in a team containing exactly k members (including themselves), and all other members of that team must also have specified the same team capacity of k.
Return a collection of teams (represented as lists of participant indices) that satisfies these constraints. Each participant must appear in exactly one team. If multiple valid team configurations exist, you may return any one of them. It is guaranteed that at least one valid solution exists for every input provided.
teamCapacity = [3,3,3,3,3,1,3][[0,1,2],[3,4,6],[5]]Participant 5 wants to be in a team of size 1, forming their own team [5]. Participants 0, 1, 2 all want teams of size 3, so they form team [0,1,2]. Similarly, participants 3, 4, 6 want teams of size 3, forming team [3,4,6]. Every participant is placed in exactly one team matching their stated preference. Other valid solutions like [[2,1,6],[5],[0,4,3]] or [[5],[0,6,2],[4,3,1]] are also acceptable.
teamCapacity = [2,1,3,3,3,2][[0,5],[1],[2,3,4]]Participant 1 forms a solo team [1] since they prefer size 1. Participants 0 and 5 both want size 2, so they form team [0,5]. Participants 2, 3, and 4 all want size 3, forming team [2,3,4]. Each team's size matches the preference of all its members.
teamCapacity = [1,1,1,1,1][[0],[1],[2],[3],[4]]All 5 participants want to be on their own individual team of size 1. The only valid configuration is for each participant to form their own team, resulting in 5 separate teams with one member each.
Constraints