forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount Nodes With the Highest Score.py
18 lines (18 loc) · 1.18 KB
/
Count Nodes With the Highest Score.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
def countHighestScoreNodes(self, parents: List[int]) -> int:
graph = collections.defaultdict(list)
for node, parent in enumerate(parents): # build graph
graph[parent].append(node)
n = len(parents) # total number of nodes
d = collections.Counter()
def count_nodes(node): # number of children node + self
p, s = 1, 0 # p: product, s: sum
for child in graph[node]: # for each child (only 2 at maximum)
res = count_nodes(child) # get its nodes count
p *= res # take the product
s += res # take the sum
p *= max(1, n - 1 - s) # times up-branch (number of nodes other than left, right children ans itself)
d[p] += 1 # count the product
return s + 1 # return number of children node + 1 (self)
count_nodes(0) # starting from root (0)
return d[max(d.keys())] # return max count