forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount Ways to Build Rooms in an Ant Colony.java
35 lines (32 loc) · 1.22 KB
/
Count Ways to Build Rooms in an Ant Colony.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
29
30
31
32
33
34
35
class Solution {
int M = (int)1e9+7;
public int waysToBuildRooms(int[] prevRoom) {
int n = prevRoom.length;
long[] fact = new long[n];
long[] invFact = new long[n];
long[] inv = new long[n];
fact[1]=fact[0]=invFact[0]=invFact[1]=inv[1]=1;
for (int i = 2; i < n; i++){ // modInverse
fact[i] = fact[i-1]*i%M;
inv[i] = M-M/i*inv[M%i]%M;
invFact[i] = invFact[i-1]*inv[i]%M;
}
Map<Integer, List<Integer>> map = new HashMap<>();
for (int i = 0; i < n; i++){ // add an edge from parent to child
map.computeIfAbsent(prevRoom[i], o -> new ArrayList<>()).add(i);
}
long[] ans = new long[]{1};
solve(0, fact, invFact, map, ans);
return (int)ans[0];
}
private int solve(int i, long[] fact, long[] invFact, Map<Integer, List<Integer>> map, long[] ans){
int sum = 0;
for (int next : map.getOrDefault(i, List.of())){
int cur = solve(next, fact, invFact, map, ans);
ans[0] = ans[0] * invFact[cur] % M; // divide fact[cur] -> multiply invFact[cur]
sum += cur;
}
ans[0] = ans[0] * fact[sum] % M;
return sum+1;
}
}