Skip to content

Commit 8a3b68a

Browse files
authored
Update 2049-count-nodes-with-the-highest-score.js
1 parent 6452998 commit 8a3b68a

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

2049-count-nodes-with-the-highest-score.js

+32
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,35 @@ const countHighestScoreNodes = function(parents) {
2424
const maxKey = Math.max(...Object.keys(hash))
2525
return hash[maxKey]
2626
};
27+
28+
// another
29+
30+
/**
31+
* @param {number[]} parents
32+
* @return {number}
33+
*/
34+
const countHighestScoreNodes = function(parents) {
35+
const n = parents.length, hash = {}, graph = {}
36+
for(let i = 1; i < n; i++) {
37+
if(graph[parents[i]] == null) graph[parents[i]] = []
38+
graph[parents[i]].push(i)
39+
}
40+
41+
dfs(0)
42+
const mk = Math.max(...Object.keys(hash))
43+
return hash[mk]
44+
45+
function dfs(i) {
46+
let num = 0, prod = 1
47+
for(const e of (graph[i] || []) ) {
48+
const tmp = dfs(e)
49+
num += tmp
50+
prod *= tmp
51+
}
52+
53+
if(n - 1 - num > 0) prod *= (n - 1 - num)
54+
hash[prod] = (hash[prod] || 0) + 1
55+
56+
return num + 1
57+
}
58+
};

0 commit comments

Comments
 (0)