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 ccc1293

Browse files
feat: add solutions to lc problem: No.2049
No.2049.Count Nodes With the Highest Score
1 parent 8a816b2 commit ccc1293

File tree

4 files changed

+208
-2
lines changed

4 files changed

+208
-2
lines changed

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

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,93 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
第一步可以将 `parents` 数组转为相对好处理的邻接表
62+
63+
接下来,观察样例 1 中的 `Removed 2`,删除一个节点可能产生若干子树,或者整棵树除掉以该节点为根的子树后剩下的部分
64+
65+
总结出规律后,递归处理即可
66+
6167
<!-- tabs:start -->
6268

6369
### **Python3**
6470

6571
<!-- 这里可写当前语言的特殊实现逻辑 -->
6672

6773
```python
68-
74+
class Solution:
75+
def countHighestScoreNodes(self, parents: List[int]) -> int:
76+
n, max_score, ans = len(parents), 0, 0
77+
g = [[] for _ in range(n)]
78+
for i in range(1, n):
79+
g[parents[i]].append(i)
80+
81+
def dfs(cur: int) -> int:
82+
nonlocal max_score, ans
83+
size, score = 1, 1
84+
for c in g[cur]:
85+
s = dfs(c)
86+
size += s
87+
score *= s
88+
if cur > 0:
89+
score *= n - size
90+
if score > max_score:
91+
max_score = score
92+
ans = 1
93+
elif score == max_score:
94+
ans += 1
95+
return size
96+
97+
dfs(0)
98+
return ans
6999
```
70100

71101
### **Java**
72102

73103
<!-- 这里可写当前语言的特殊实现逻辑 -->
74104

75105
```java
106+
class Solution {
107+
108+
private int n;
109+
private long maxScore;
110+
private int ans;
111+
private List<List<Integer>> graph;
112+
113+
public int countHighestScoreNodes(int[] parents) {
114+
n = parents.length;
115+
maxScore = 0;
116+
ans = 0;
117+
graph = new ArrayList<>();
118+
for (int i = 0; i < n; i++) {
119+
graph.add(new ArrayList<>());
120+
}
121+
for (int i = 1; i < n; i++) {
122+
graph.get(parents[i]).add(i);
123+
}
124+
dfs(0);
125+
return ans;
126+
}
76127

128+
private int dfs(int cur) {
129+
int size = 1;
130+
long score = 1;
131+
for (int child : graph.get(cur)) {
132+
int s = dfs(child);
133+
size += s;
134+
score *= s;
135+
}
136+
if (cur > 0) {
137+
score *= n - size;
138+
}
139+
if (score > maxScore) {
140+
maxScore = score;
141+
ans = 1;
142+
} else if (score == maxScore) {
143+
ans++;
144+
}
145+
return size;
146+
}
147+
}
77148
```
78149

79150
### **TypeScript**

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

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,78 @@ The highest score is 2, and two nodes (node 0 and node 1) have the highest score
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def countHighestScoreNodes(self, parents: List[int]) -> int:
60+
n, max_score, ans = len(parents), 0, 0
61+
g = [[] for _ in range(n)]
62+
for i in range(1, n):
63+
g[parents[i]].append(i)
64+
65+
def dfs(cur: int) -> int:
66+
nonlocal max_score, ans
67+
size, score = 1, 1
68+
for c in g[cur]:
69+
s = dfs(c)
70+
size += s
71+
score *= s
72+
if cur > 0:
73+
score *= n - size
74+
if score > max_score:
75+
max_score = score
76+
ans = 1
77+
elif score == max_score:
78+
ans += 1
79+
return size
80+
81+
dfs(0)
82+
return ans
5983
```
6084

6185
### **Java**
6286

6387
```java
88+
class Solution {
89+
90+
private int n;
91+
private long maxScore;
92+
private int ans;
93+
private List<List<Integer>> graph;
94+
95+
public int countHighestScoreNodes(int[] parents) {
96+
n = parents.length;
97+
maxScore = 0;
98+
ans = 0;
99+
graph = new ArrayList<>();
100+
for (int i = 0; i < n; i++) {
101+
graph.add(new ArrayList<>());
102+
}
103+
for (int i = 1; i < n; i++) {
104+
graph.get(parents[i]).add(i);
105+
}
106+
dfs(0);
107+
return ans;
108+
}
64109

110+
private int dfs(int cur) {
111+
int size = 1;
112+
long score = 1;
113+
for (int child : graph.get(cur)) {
114+
int s = dfs(child);
115+
size += s;
116+
score *= s;
117+
}
118+
if (cur > 0) {
119+
score *= n - size;
120+
}
121+
if (score > maxScore) {
122+
maxScore = score;
123+
ans = 1;
124+
} else if (score == maxScore) {
125+
ans++;
126+
}
127+
return size;
128+
}
129+
}
65130
```
66131

67132
### **TypeScript**
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
3+
private int n;
4+
private long maxScore;
5+
private int ans;
6+
private List<List<Integer>> graph;
7+
8+
public int countHighestScoreNodes(int[] parents) {
9+
n = parents.length;
10+
maxScore = 0;
11+
ans = 0;
12+
graph = new ArrayList<>();
13+
for (int i = 0; i < n; i++) {
14+
graph.add(new ArrayList<>());
15+
}
16+
for (int i = 1; i < n; i++) {
17+
graph.get(parents[i]).add(i);
18+
}
19+
dfs(0);
20+
return ans;
21+
}
22+
23+
private int dfs(int cur) {
24+
int size = 1;
25+
long score = 1;
26+
for (int child : graph.get(cur)) {
27+
int s = dfs(child);
28+
size += s;
29+
score *= s;
30+
}
31+
if (cur > 0) {
32+
score *= n - size;
33+
}
34+
if (score > maxScore) {
35+
maxScore = score;
36+
ans = 1;
37+
} else if (score == maxScore) {
38+
ans++;
39+
}
40+
return size;
41+
}
42+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def countHighestScoreNodes(self, parents: List[int]) -> int:
6+
n, max_score, ans = len(parents), 0, 0
7+
g = [[] for _ in range(n)]
8+
for i in range(1, n):
9+
g[parents[i]].append(i)
10+
11+
def dfs(cur: int) -> int:
12+
nonlocal max_score, ans
13+
size, score = 1, 1
14+
for c in g[cur]:
15+
s = dfs(c)
16+
size += s
17+
score *= s
18+
if cur > 0:
19+
score *= n - size
20+
if score > max_score:
21+
max_score = score
22+
ans = 1
23+
elif score == max_score:
24+
ans += 1
25+
return size
26+
27+
dfs(0)
28+
return ans

0 commit comments

Comments
(0)

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