Number of Ways to Wear Different Hats to Each Other
Real-world version: Handing Out Badges Nobody SharesThe statement here is our own: the same inputs, outputs and technique.
There are some staff, and hats[i] lists the badge numbers that would suit staff member i. Every list holds distinct numbers.
Every staff member is given exactly one badge that suits them, and no two are given the same badge.
Return how many ways that can be done, modulo 10^9 + 7.
Examples
Example 1
- Input
- hats = [[1, 2], [1, 2]]
- Output
- 2
Both staff would take either badge, so one takes 1 and the other 2, or the other way round.
Example 2
- Input
- hats = [[1], [1]]
- Output
- 0
Both staff would only take badge 1, and it cannot be shared.
Example 3
- Input
- hats = [[1, 2, 3, 4], [2, 3], [3]]
- Output
- 2
Badge 3 must go to the third staff member, so badge 2 must go to the second, and the first is left with badge 1 or 4.
Constraints
- 1 <= hats.length <= 10
- 1 <= hats[i].length <= 40
- 1 <= hats[i][j] <= 40
- The numbers in hats[i] are all different
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def number_ways(hats: list[list[int]]) -> int:public int numberWays(List<List<Integer>> hats)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Beautiful ArrangementReal-world version: Divisible Crate PlacementsMedium
- Partition to K Equal Sum SubsetsReal-world version: Dealing Counterweights Onto HoistsMedium
- Shopping OffersReal-world version: Cheapest Basket at the Fastener CounterMedium
- Matchsticks to SquareReal-world version: Fencing a Square From Every RodMedium