Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7a277f7

Browse files
Update 1916.Count Ways to Build Rooms in an Ant Colony.py
1 parent c4681fa commit 7a277f7

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed
Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
1-
from collections import defaultdict
2-
from math import comb
3-
from typing import List
4-
5-
61
class Solution:
7-
def waysToBuildRooms(self, prevRoom: List[int]) -> int:
8-
modulo = 10**9 + 7
9-
ingoing = defaultdict(set)
10-
outgoing = defaultdict(set)
2+
def waysToBuildRooms(self, prevRoom: List[int]) -> int:
3+
modulo = 10**9 + 7
4+
ingoing = defaultdict(set)
5+
outgoing = defaultdict(set)
116

12-
for i in range(1, len(prevRoom)):
13-
ingoing[i].add(prevRoom[i])
14-
outgoing[prevRoom[i]].add(i)
15-
ans = [1]
7+
for i in range(1, len(prevRoom)):
8+
ingoing[i].add(prevRoom[i])
9+
outgoing[prevRoom[i]].add(i)
10+
ans = [1]
1611

17-
def recurse(i):
18-
if len(outgoing[i]) == 0:
19-
return1# just self
12+
def recurse(i):
13+
if len(outgoing[i]) == 0:
14+
return1
2015

21-
nodes_in_tree = 0
22-
for v in outgoing[i]:
23-
cn = recurse(v)
24-
if nodes_in_tree != 0:
25-
ans[0] *= comb(nodes_in_tree + cn, cn)
26-
ans[0] %= modulo
27-
nodes_in_tree += cn
28-
return nodes_in_tree + 1
16+
nodes_in_tree = 0
17+
for v in outgoing[i]:
18+
cn = recurse(v)
19+
if nodes_in_tree != 0:
20+
ans[0] *= comb(nodes_in_tree + cn, cn)
21+
ans[0] %= modulo
22+
nodes_in_tree += cn
23+
return nodes_in_tree + 1
2924

30-
recurse(0)
31-
return ans[0] % modulo
25+
recurse(0)
26+
return ans[0] % modulo

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /