Skip to content

Commit 8a816b2

Browse files
authored
feat: add typescript solution to lc problem: No.2049 (doocs#596)
lc No.2049.Count Nodes With the Highest Score
1 parent 648bfde commit 8a816b2

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

solution/2000-2099/2049.Count Nodes With the Highest Score/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,45 @@
7676

7777
```
7878

79+
### **TypeScript**
80+
81+
```ts
82+
function countHighestScoreNodes(parents: number[]): number {
83+
const n = parents.length;
84+
let edge = Array.from({length: n}, (v, i) => ([]));
85+
for (let i = 0; i < n; i++) {
86+
const parent = parents[i];
87+
if (parent != -1) {
88+
edge[parent].push(i);
89+
}
90+
}
91+
92+
let ans = 0;
93+
let max = 0;
94+
function dfs(idx: number): number {
95+
let size = 1, score = 1;
96+
for (let i = 0; i < edge[idx].length; i++) {
97+
const child = edge[idx][i];
98+
let childSize = dfs(child);
99+
size += childSize;
100+
score *= childSize;
101+
}
102+
if (idx > 0) {
103+
score *= (n - size);
104+
}
105+
if (score > max) {
106+
max = score;
107+
ans = 1;
108+
} else if (score == max) {
109+
ans++;
110+
}
111+
return size;
112+
}
113+
dfs(0);
114+
return ans;
115+
};
116+
```
117+
79118
### **...**
80119

81120
```

solution/2000-2099/2049.Count Nodes With the Highest Score/README_EN.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,45 @@ The highest score is 2, and two nodes (node 0 and node 1) have the highest score
6464

6565
```
6666

67+
### **TypeScript**
68+
69+
```ts
70+
function countHighestScoreNodes(parents: number[]): number {
71+
const n = parents.length;
72+
let edge = Array.from({length: n}, (v, i) => ([]));
73+
for (let i = 0; i < n; i++) {
74+
const parent = parents[i];
75+
if (parent != -1) {
76+
edge[parent].push(i);
77+
}
78+
}
79+
80+
let ans = 0;
81+
let max = 0;
82+
function dfs(idx: number): number {
83+
let size = 1, score = 1;
84+
for (let i = 0; i < edge[idx].length; i++) {
85+
const child = edge[idx][i];
86+
let childSize = dfs(child);
87+
size += childSize;
88+
score *= childSize;
89+
}
90+
if (idx > 0) {
91+
score *= (n - size);
92+
}
93+
if (score > max) {
94+
max = score;
95+
ans = 1;
96+
} else if (score == max) {
97+
ans++;
98+
}
99+
return size;
100+
}
101+
dfs(0);
102+
return ans;
103+
};
104+
```
105+
67106
### **...**
68107

69108
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function countHighestScoreNodes(parents: number[]): number {
2+
const n = parents.length;
3+
let edge = Array.from({length: n}, (v, i) => ([]));
4+
for (let i = 0; i < n; i++) {
5+
const parent = parents[i];
6+
if (parent != -1) {
7+
edge[parent].push(i);
8+
}
9+
}
10+
11+
let ans = 0;
12+
let max = 0;
13+
function dfs(idx: number): number {
14+
let size = 1, score = 1;
15+
for (let i = 0; i < edge[idx].length; i++) {
16+
const child = edge[idx][i];
17+
let childSize = dfs(child);
18+
size += childSize;
19+
score *= childSize;
20+
}
21+
if (idx > 0) {
22+
score *= (n - size);
23+
}
24+
if (score > max) {
25+
max = score;
26+
ans = 1;
27+
} else if (score == max) {
28+
ans++;
29+
}
30+
return size;
31+
}
32+
dfs(0);
33+
return ans;
34+
};

0 commit comments

Comments
 (0)