Group the People Given the Group Size They Belong To - Problem

Imagine you're organizing a team-building event where n people need to be divided into groups based on their preferences. Each person has a unique ID from 0 to n-1, and they've told you exactly what group size they want to be in.

You're given an array groupSizes where groupSizes[i] represents the desired group size for person i. For example, if groupSizes[1] = 3, then person 1 wants to be in a group of exactly 3 people.

Your goal: Create groups where every person gets their desired group size. Each person must appear in exactly one group, and the actual size of each group must match what its members requested.

Since multiple valid groupings might exist, you can return any valid solution. The problem guarantees that at least one solution always exists.

Input & Output

example_1.py โ€” Basic Grouping
$ Input: groupSizes = [3,3,3,3,3,1,3]
โ€บ Output: [[5],[0,1,2],[3,4,6]]
๐Ÿ’ก Note: Person 5 wants to be in a group of size 1, so they form their own group. People 0,1,2 all want groups of size 3 and can be grouped together. Similarly, people 3,4,6 form another group of size 3. The grouping satisfies everyone's preferences.
example_2.py โ€” Mixed Group Sizes
$ Input: groupSizes = [2,1,3,3,3,2]
โ€บ Output: [[1],[0,5],[2,3,4]]
๐Ÿ’ก Note: Person 1 wants a group of size 1, so they're alone. People 0 and 5 both want groups of size 2, so they pair up. People 2,3,4 all want groups of size 3, forming the final group. Each person gets exactly the group size they requested.
example_3.py โ€” All Same Size
$ Input: groupSizes = [2,2,2,2]
โ€บ Output: [[0,1],[2,3]]
๐Ÿ’ก Note: Everyone wants to be in groups of size 2. We can form two groups: [0,1] and [2,3]. Both groups have exactly 2 members as requested by all participants.

Constraints

  • 1 โ‰ค groupSizes.length โ‰ค 500
  • 1 โ‰ค groupSizes[i] โ‰ค groupSizes.length
  • Important: It's guaranteed that there will be at least one valid solution

Visualization

Tap to expand
Restaurant Table Assignment: Group People by PreferencesCustomers with Table Size Preferences0wants 31wants 32wants 13wants 34wants 25wants 2Table Assignment ProcessStep 1: Customer 0 (wants 3) โ†’ Start new table for 3 people: [0, _, _]Step 2: Customer 1 (wants 3) โ†’ Add to table for 3: [0, 1, _]Step 3: Customer 2 (wants 1) โ†’ Complete individual table: [2] โœ“Step 4: Customer 3 (wants 3) โ†’ Complete table for 3: [0, 1, 3] โœ“Final Table ArrangementTable for 1Customer 2Table for 3Customers 0, 1, 3Table for 2Customers 4, 5๐Ÿ’ก Key Insight: Hash Map as Restaurant Hostโ€ข Keep track of partially filled tables by size (hash map: size โ†’ people list)โ€ข When a table fills up completely, seat the group and clear that table slotEveryone gets exactly the table size they requested! O(n) time, optimal solution.
Understanding the Visualization
1
Customer Arrives
Each customer tells you their preferred table size (like person i wanting groupSizes[i])
2
Find Available Table
Check if there's a partially filled table of that size, or start a new one
3
Seat Customer
Add the customer to the appropriate table
4
Complete Tables
When a table reaches its target size, it's complete - move to next customers
Key Takeaway
๐ŸŽฏ Key Insight: Use a hash map to track partially filled groups by size. Complete and output groups immediately when they reach target size, making this an optimal O(n) solution.
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
52.3K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen