Skip to content

Commit 1ca96b8

Browse files
authored
Update README.md for 1916
1 parent 9dcc160 commit 1ca96b8

File tree

1 file changed

+26
-1
lines changed
  • solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony

1 file changed

+26
-1
lines changed

solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,32 @@ tags:
8080
#### Python3
8181

8282
```python
83-
83+
class Solution:
84+
def waysToBuildRooms(self, prevRoom: List[int]) -> int:
85+
modulo = 10 ** 9 + 7
86+
ingoing = defaultdict(set)
87+
outgoing = defaultdict(set)
88+
89+
for i in range(1, len(prevRoom)):
90+
ingoing[i].add(prevRoom[i])
91+
outgoing[prevRoom[i]].add(i)
92+
ans = [1]
93+
94+
def recurse(i):
95+
if len(outgoing[i]) == 0:
96+
return 1 # just self
97+
98+
nodes_in_tree = 0
99+
for v in outgoing[i]:
100+
cn = recurse(v)
101+
if nodes_in_tree != 0:
102+
ans[0] *= comb(nodes_in_tree + cn, cn)
103+
ans[0] %= modulo
104+
nodes_in_tree += cn
105+
return nodes_in_tree + 1
106+
107+
recurse(0)
108+
return ans[0] % modulo
84109
```
85110

86111
#### Java

0 commit comments

Comments
 (0)