Ctrl K

Teams by Requested Size

Medium
A hackathon has n participants numbered from 0 to n - 1. Each of them said in advance what team size they want: sizes[i] is the team size participant i asked for. The organizer forms teams with a simple rule. Everyone who asked for a team of size k lines up by increasing number; the first k people in the line make one team, the next k make another, and so on. Return the list of all teams, each as a list of its members' numbers. The order of the teams and the order of numbers inside a team do not matter. The input guarantees that for every size k, the number of people who asked for it is a multiple of k, so everyone ends up on a team.
Example 1
sizes=[2, 1, 2, 3, 3, 3] → [[0, 2], [1], [3, 4, 5]]

Participant 1 wants to work alone: team [1]. Participants 0 and 2 form the pair, and 3, 4, 5 form the trio.

Example 2
sizes=[2, 2, 2, 2] → [[0, 1], [2, 3]]

Everyone wants a pair. Going by number: first [0, 1], then [2, 3]. The answer [[0, 2], [1, 3]] is wrong because it breaks the queue rule.

Example 3
sizes=[1] → [[0]]
Constraints
  • 1 ≤ len(sizes) ≤ 500
  • 1 ≤ sizes[i] ≤ len(sizes)
  • For every k, the number of participants with sizes[i] = k is a multiple of k
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsJobs With a Cooldown MediumSorted Into Runs of Three MediumFewest Guests by Lanyard Color MediumDeal Into Consecutive Sets MediumPair Each Number With Its Double Medium
loading Python…
Input
sizes=[2, 1, 2, 3, 3, 3]
Expected
[[0, 2], [1], [3, 4, 5]]

Run checks the first 3 tests in your browser. Submit checks all 41 tests on the server, hidden ones included.