forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1434.java
28 lines (26 loc) · 875 Bytes
/
1434.java
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
class Solution {
public int numberWays(List<List<Integer>> hats) {
int n = hats.size(), mod = 1000000007;
int[][] f = new int[2][1024];
f[0][0] = 1;
List<Integer>[] a = new List[41];
for (int i = 1; i <= 40; i++) a[i] = new ArrayList();
for (int i = 0; i < n; i++) {
for (int hat : hats.get(i)) {
a[hat].add(i);
}
}
for (int i = 1; i <= 40; i++) {
for (int j = 0; j < (1 << n); j++) {
f[i % 2][j] = f[(i - 1) % 2][j];
for (int x : a[i]) {
if (((j >> x) & 1) != 0) {
f[i % 2][j] += f[(i - 1) % 2][j ^ (1 << x)];
f[i % 2][j] %= mod;
}
}
}
}
return f[0][(1 << n) - 1];
}
}