1434. Number of Ways to Wear Different Hats to Each Other Hard

@problem@discussion
#Array#Dynamic Programming#Bit Manipulation#Bitmask



1/**
2 * [1434] Number of Ways to Wear Different Hats to Each Other
3 *
4 * There are n people and 40 types of hats labeled from 1 to 40.
5 * Given a 2D integer array hats, where hats[i] is a list of all hats preferred by the i^th person.
6 * Return the number of ways that the n people wear different hats to each other.
7 * Since the answer may be too large, return it modulo 10^9 + 7.
8 *  
9 * Example 1:
10 * 
11 * Input: hats = [[3,4],[4,5],[5]]
12 * Output: 1
13 * Explanation: There is only one way to choose hats given the conditions. 
14 * First person choose hat 3, Second person choose hat 4 and last one hat 5.
15 * 
16 * Example 2:
17 * 
18 * Input: hats = [[3,5,1],[3,5]]
19 * Output: 4
20 * Explanation: There are 4 ways to choose hats:
21 * (3,5), (5,3), (1,3) and (1,5)
22 * 
23 * Example 3:
24 * 
25 * Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
26 * Output: 24
27 * Explanation: Each person can choose hats labeled from 1 to 4.
28 * Number of Permutations of (1,2,3,4) = 24.
29 * 
30 *  
31 * Constraints:
32 * 
33 * 	n == hats.length
34 * 	1 <= n <= 10
35 * 	1 <= hats[i].length <= 40
36 * 	1 <= hats[i][j] <= 40
37 * 	hats[i] contains a list of unique integers.
38 * 
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/number-of-ways-to-wear-different-hats-to-each-other/
43// discuss: https://leetcode.com/problems/number-of-ways-to-wear-different-hats-to-each-other/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48    pub fn number_ways(hats: Vec<Vec<i32>>) -> i32 {
49        0
50    }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_1434() {
61    }
62}
63


Back
© 2025 bowen.ge All Rights Reserved.