File tree Expand file tree Collapse file tree 1 file changed +26
-1
lines changed
solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony Expand file tree Collapse file tree 1 file changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -104,7 +104,32 @@ tags:
104
104
#### Python3
105
105
106
106
``` python
107
-
107
+ class Solution :
108
+ def waysToBuildRooms (self , prevRoom : List[int ]) -> int :
109
+ modulo = 10 ** 9 + 7
110
+ ingoing = defaultdict(set )
111
+ outgoing = defaultdict(set )
112
+
113
+ for i in range (1 , len (prevRoom)):
114
+ ingoing[i].add(prevRoom[i])
115
+ outgoing[prevRoom[i]].add(i)
116
+ ans = [1 ]
117
+
118
+ def recurse (i ):
119
+ if len (outgoing[i]) == 0 :
120
+ return 1 # just self
121
+
122
+ nodes_in_tree = 0
123
+ for v in outgoing[i]:
124
+ cn = recurse(v)
125
+ if nodes_in_tree != 0 :
126
+ ans[0 ] *= comb(nodes_in_tree + cn, cn)
127
+ ans[0 ] %= modulo
128
+ nodes_in_tree += cn
129
+ return nodes_in_tree + 1
130
+
131
+ recurse(0 )
132
+ return ans[0 ] % modulo
108
133
```
109
134
110
135
#### Java
You can’t perform that action at this time.
0 commit comments