Number of Ways to Wear Different Hats to Each Other - Problem

You're organizing a costume party where n people need to wear different hats! There are 40 types of hats available (labeled 1 to 40), and each person has their own list of preferred hats they're willing to wear.

Your challenge: Count how many ways all n people can wear different hats from each other, where each person wears exactly one hat from their preference list, and no two people wear the same hat.

Input: A 2D array hats where hats[i] contains all hat types that person i is willing to wear.

Output: The number of valid arrangements modulo 109 + 7.

Example: If person 0 likes hats [1,2] and person 1 likes hats [2,3], there are 2 ways: person 0 wears hat 1 & person 1 wears hat 2, OR person 0 wears hat 1 & person 1 wears hat 3.

Input & Output

example_1.py โ€” Basic Case
$ Input: hats = [[3,4],[4,5],[5]]
โ€บ Output: 1
๐Ÿ’ก Note: There's only 1 way: person 0 wears hat 3, person 1 wears hat 4, and person 2 wears hat 5.
example_2.py โ€” Multiple Options
$ Input: hats = [[3,5,1],[3,5],[1,3,5,4],[9,4]]
โ€บ Output: 2
๐Ÿ’ก Note: Two valid arrangements exist: [1,3,5,4] and [1,5,3,4] where each person gets a different hat from their preferences.
example_3.py โ€” Overlapping Preferences
$ Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
โ€บ Output: 24
๐Ÿ’ก Note: Each person can wear any of 4 hats, so we have 4! = 24 different ways to assign unique hats.

Visualization

Tap to expand
Hat to People Assignment VisualizationHat 1Hat 2Hat 3P1P2P3likeslikesBitmask State100P1 assignedP2 freeP3 freeDP Transition: dp(hat, mask) โ†’ dp(hat+1, new_mask)For each hat: Skip it OR assign to available personUpdate bitmask and continue to next hat
Understanding the Visualization
1
Map Hats to People
Create a reverse mapping: for each hat, list all people who can wear it
2
Use Bitmask for State
Each bit represents whether a person has been assigned a hat (1) or not (0)
3
Process Hats Sequentially
For each hat 1-40, either skip it or assign it to an unassigned person
4
Count Valid Assignments
When all people are assigned (mask = 111...1), we found a valid solution
Key Takeaway
๐ŸŽฏ Key Insight: Process hats sequentially and use bitmasks to track people assignments, avoiding the complexity of people-to-hat assignments!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(40 * 2^n * n)

40 hats, 2^n possible masks, and up to n people can wear each hat

n
2n
โš  Quadratic Growth
Space Complexity
O(40 * 2^n)

DP table with 40 hats and 2^n possible people assignments

n
2n
โš  Quadratic Space

Constraints

  • n == hats.length
  • 1 โ‰ค n โ‰ค 10
  • 1 โ‰ค hats[i].length โ‰ค 40
  • 1 โ‰ค hats[i][j] โ‰ค 40
  • All values in hats[i] are unique
  • The total number of hats across all people is at most 40
Asked in
Google 45 Facebook 32 Amazon 28 Microsoft 22
28.5K Views
Medium Frequency
~35 min Avg. Time
892 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