-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1434-number-of-ways-to-wear-different-hats-to-each-other.js
53 lines (45 loc) · 1.33 KB
/
1434-number-of-ways-to-wear-different-hats-to-each-other.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* 1434. Number of Ways to Wear Different Hats to Each Other
* https://leetcode.com/problems/number-of-ways-to-wear-different-hats-to-each-other/
* Difficulty: Hard
*
* There are n people and 40 types of hats labeled from 1 to 40.
*
* Given a 2D integer array hats, where hats[i] is a list of all hats preferred by the ith person.
*
* Return the number of ways that n people can wear different hats from each other.
*
* Since the answer may be too large, return it modulo 109 + 7.
*/
/**
* @param {number[][]} hats
* @return {number}
*/
var numberWays = function(hats) {
const MOD = 1e9 + 7;
const hatToPeople = Array(41).fill().map(() => []);
const cache = new Map();
for (let person = 0; person < hats.length; person++) {
for (const hat of hats[person]) {
hatToPeople[hat].push(person);
}
}
function assignHats(hat, usedMask) {
if (hat > 40) {
return usedMask === (1 << hats.length) - 1 ? 1 : 0;
}
const key = `${hat}:${usedMask}`;
if (cache.has(key)) {
return cache.get(key);
}
let ways = assignHats(hat + 1, usedMask);
for (const person of hatToPeople[hat]) {
if (!(usedMask & (1 << person))) {
ways = (ways + assignHats(hat + 1, usedMask | (1 << person))) % MOD;
}
}
cache.set(key, ways);
return ways;
}
return assignHats(1, 0);
};