Skip to content

Commit c4681fa

Browse files
authored
Update README_EN.md for 1916
1 parent 1ca96b8 commit c4681fa

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_EN.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,32 @@ tags:
104104
#### Python3
105105

106106
```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
108133
```
109134

110135
#### Java

0 commit comments

Comments
 (0)