File tree 1 file changed +21
-26
lines changed
1 file changed +21
-26
lines changed Original file line number Diff line number Diff line change 1
- from collections import defaultdict
2
- from math import comb
3
- from typing import List
4
-
5
-
6
1
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 )
11
6
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 ]
16
11
17
- def recurse (i ):
18
- if len (outgoing [i ]) == 0 :
19
- return 1 # just self
12
+ def recurse (i ):
13
+ if len (outgoing [i ]) == 0 :
14
+ return 1
20
15
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
29
24
30
- recurse (0 )
31
- return ans [0 ] % modulo
25
+ recurse (0 )
26
+ return ans [0 ] % modulo
You can’t perform that action at this time.
0 commit comments