Skip to content

Commit 91f81d5

Browse files
authored
feat: add solutions to lc problem: No.2049 (#2096)
No.2049.Count Nodes With the Highest Score
1 parent 8669e5b commit 91f81d5

File tree

8 files changed

+583
-406
lines changed

8 files changed

+583
-406
lines changed

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

Lines changed: 197 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,27 @@
5858

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

61-
第一步可以将 `parents` 数组转为相对好处理的邻接矩阵。
61+
**方法一:DFS**
6262

63-
接下来,观察样例 1 中的 `Removed 2`,删除一个节点可能产生若干子树,或者整棵树除掉以该节点为根的子树后剩下的部分
63+
我们先根据给定的父节点数组 `parents` 构建图 $g$,其中 $g[i]$ 表示节点 $i$ 的所有子节点。定义变量 $ans$ 表示最高得分的节点数目,变量 $mx$ 表示最高得分
6464

65-
总结出规律后,递归处理即可。
65+
然后,我们设计一个函数 $dfs(i, fa)$,它的作用是计算节点 $i$ 的分数,并返回以节点 $i$ 为根的子树的节点数目。
66+
67+
函数 $dfs(i, fa)$ 的计算过程如下:
68+
69+
我们首先初始化变量 $cnt = 1$,表示以节点 $i$ 为根的子树的节点数目;变量 $score = 1$,表示以节点 $i$ 初始分数。
70+
71+
接下来,我们遍历节点 $i$ 的所有子节点 $j$,如果 $j$ 不是节点 $i$ 的父节点 $fa$,那么我们递归调用 $dfs(j, i)$,并将返回值累乘到 $score$ 中,同时将返回值累加到 $cnt$ 中。
72+
73+
遍历完子节点后,如果 $n - cnt > 0$,那么我们将 $n - cnt$ 累乘到 $score$ 中。
74+
75+
然后,我们判断 $mx$ 是否小于 $score$,如果小于,那么我们将 $mx$ 更新为 $score$,并将 $ans$ 更新为 $1$;如果等于,那么我们将 $ans$ 更新为 $ans + 1$。
76+
77+
最后,我们返回 $cnt$。
78+
79+
最终,我们调用 $dfs(0, -1)$,并返回 $ans$。
80+
81+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是节点数目。
6682

6783
<!-- tabs:start -->
6884

@@ -73,28 +89,29 @@
7389
```python
7490
class Solution:
7591
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+
def dfs(i: int, fa: int):
93+
cnt = score = 1
94+
for j in g[i]:
95+
if j != fa:
96+
t = dfs(j, i)
97+
score *= t
98+
cnt += t
99+
if n - cnt:
100+
score *= n - cnt
101+
nonlocal ans, mx
102+
if mx < score:
103+
mx = score
92104
ans = 1
93-
elif score == max_score:
105+
elif mx == score:
94106
ans += 1
95-
return size
107+
return cnt
96108

97-
dfs(0)
109+
n = len(parents)
110+
g = [[] for _ in range(n)]
111+
for i in range(1, n):
112+
g[parents[i]].append(i)
113+
ans = mx = 0
114+
dfs(0, -1)
98115
return ans
99116
```
100117

@@ -104,158 +121,205 @@ class Solution:
104121

105122
```java
106123
class Solution {
107-
108-
private int n;
109-
private long maxScore;
124+
private List<Integer>[] g;
110125
private int ans;
111-
private List<List<Integer>> graph;
126+
private long mx;
127+
private int n;
112128

113129
public int countHighestScoreNodes(int[] parents) {
114130
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);
131+
g = new List[n];
132+
Arrays.setAll(g, i -> new ArrayList<>());
133+
for (int i = 1; i < n; ++i) {
134+
g[parents[i]].add(i);
123135
}
124-
dfs(0);
136+
dfs(0, -1);
125137
return ans;
126138
}
127139

128-
private int dfs(int cur) {
129-
int size = 1;
140+
private int dfs(int i, int fa) {
141+
int cnt = 1;
130142
long score = 1;
131-
for (int child : graph.get(cur)) {
132-
int s = dfs(child);
133-
size += s;
134-
score *= s;
143+
for (int j : g[i]) {
144+
if (j != fa) {
145+
int t = dfs(j, i);
146+
cnt += t;
147+
score *= t;
148+
}
135149
}
136-
if (cur > 0) {
137-
score *= n - size;
150+
if (n - cnt > 0) {
151+
score *= n - cnt;
138152
}
139-
if (score > maxScore) {
140-
maxScore = score;
153+
if (mx < score) {
154+
mx = score;
141155
ans = 1;
142-
} else if (score == maxScore) {
143-
ans++;
156+
} else if (mx == score) {
157+
++ans;
144158
}
145-
return size;
159+
return cnt;
146160
}
147161
}
148162
```
149163

164+
### **C++**
165+
166+
```cpp
167+
class Solution {
168+
public:
169+
int countHighestScoreNodes(vector<int>& parents) {
170+
int n = parents.size();
171+
vector<int> g[n];
172+
for (int i = 1; i < n; ++i) {
173+
g[parents[i]].push_back(i);
174+
}
175+
int ans = 0;
176+
long long mx = 0;
177+
function<int(int, int)> dfs = [&](int i, int fa) {
178+
long long score = 1;
179+
int cnt = 1;
180+
for (int j : g[i]) {
181+
if (j != fa) {
182+
int t = dfs(j, i);
183+
cnt += t;
184+
score *= t;
185+
}
186+
}
187+
if (n - cnt) {
188+
score *= n - cnt;
189+
}
190+
if (mx < score) {
191+
mx = score;
192+
ans = 1;
193+
} else if (mx == score) {
194+
++ans;
195+
}
196+
return cnt;
197+
};
198+
dfs(0, -1);
199+
return ans;
200+
}
201+
};
202+
```
203+
204+
### **Go**
205+
206+
```go
207+
func countHighestScoreNodes(parents []int) (ans int) {
208+
n := len(parents)
209+
g := make([][]int, n)
210+
for i := 1; i < n; i++ {
211+
g[parents[i]] = append(g[parents[i]], i)
212+
}
213+
mx := 0
214+
var dfs func(i, fa int) int
215+
dfs = func(i, fa int) int {
216+
cnt, score := 1, 1
217+
for _, j := range g[i] {
218+
if j != fa {
219+
t := dfs(j, i)
220+
cnt += t
221+
score *= t
222+
}
223+
}
224+
if n-cnt > 0 {
225+
score *= n - cnt
226+
}
227+
if mx < score {
228+
mx = score
229+
ans = 1
230+
} else if mx == score {
231+
ans++
232+
}
233+
return cnt
234+
}
235+
dfs(0, -1)
236+
return
237+
}
238+
```
239+
150240
### **TypeScript**
151241

152242
```ts
153243
function countHighestScoreNodes(parents: number[]): number {
154244
const n = parents.length;
155-
let edge = Array.from({ length: n }, (v, i) => []);
156-
for (let i = 0; i < n; i++) {
157-
const parent = parents[i];
158-
if (parent != -1) {
159-
edge[parent].push(i);
160-
}
245+
const g: number[][] = Array.from({ length: n }, () => []);
246+
for (let i = 1; i < n; i++) {
247+
g[parents[i]].push(i);
161248
}
162-
163-
let ans = 0;
164-
let max = 0;
165-
function dfs(idx: number): number {
166-
let size = 1,
167-
score = 1;
168-
for (let i = 0; i < edge[idx].length; i++) {
169-
const child = edge[idx][i];
170-
let childSize = dfs(child);
171-
size += childSize;
172-
score *= childSize;
249+
let [ans, mx] = [0, 0];
250+
const dfs = (i: number, fa: number): number => {
251+
let [cnt, score] = [1, 1];
252+
for (const j of g[i]) {
253+
if (j !== fa) {
254+
const t = dfs(j, i);
255+
cnt += t;
256+
score *= t;
257+
}
173258
}
174-
if (idx > 0) {
175-
score *= n - size;
259+
if (n - cnt) {
260+
score *= n - cnt;
176261
}
177-
if (score > max) {
178-
max = score;
262+
if (mx < score) {
263+
mx = score;
179264
ans = 1;
180-
} else if (score == max) {
265+
} else if (mx === score) {
181266
ans++;
182267
}
183-
return size;
184-
}
185-
dfs(0);
268+
return cnt;
269+
};
270+
dfs(0, -1);
186271
return ans;
187272
}
188273
```
189274

190-
### **C++**
275+
### **C#**
191276

192-
```cpp
193-
class Solution {
194-
public:
195-
int ans;
196-
long long maxScore;
197-
int n;
277+
```cs
278+
public class Solution {
279+
private List<int>[] g;
280+
private int ans;
281+
private long mx;
282+
private int n;
198283

199-
int countHighestScoreNodes(vector<int>& parents) {
200-
ans = 0;
201-
maxScore = 0;
202-
n = parents.size();
203-
unordered_map<int, vector<int>> g;
204-
for (int i = 1; i < n; ++i) g[parents[i]].push_back(i);
205-
dfs(0, g);
284+
public int CountHighestScoreNodes(int[] parents) {
285+
n = parents.Length;
286+
g = new List<int>[n];
287+
for (int i = 0; i < n; ++i) {
288+
g[i] = new List<int>();
289+
}
290+
for (int i = 1; i < n; ++i) {
291+
g[parents[i]].Add(i);
292+
}
293+
294+
Dfs(0, -1);
206295
return ans;
207296
}
208297

209-
int dfs(int u, unordered_map<int, vector<int>>& g) {
210-
int size = 1;
211-
long long score = 1;
212-
for (int v : g[u]) {
213-
int t = dfs(v, g);
214-
size += t;
215-
score *= t;
298+
private int Dfs(int i, int fa) {
299+
int cnt = 1;
300+
long score = 1;
301+
302+
foreach (int j in g[i]) {
303+
if (j != fa) {
304+
int t = Dfs(j, i);
305+
cnt += t;
306+
score *= t;
307+
}
216308
}
217-
if (u > 0) score *= (n - size);
218-
if (score > maxScore) {
219-
maxScore = score;
309+
310+
if (n - cnt > 0) {
311+
score *= n - cnt;
312+
}
313+
314+
if (mx < score) {
315+
mx = score;
220316
ans = 1;
221-
} else if (score == maxScore)
317+
} else if (mx == score) {
222318
++ans;
223-
return size;
224-
}
225-
};
226-
```
227-
228-
### **Go**
319+
}
229320

230-
```go
231-
func countHighestScoreNodes(parents []int) int {
232-
n := len(parents)
233-
g := make([][]int, n)
234-
for i := 1; i < n; i++ {
235-
p := parents[i]
236-
g[p] = append(g[p], i)
237-
}
238-
maxScore, ans := 0, 0
239-
var dfs func(int) int
240-
dfs = func(u int) int {
241-
size, score := 1, 1
242-
for _, v := range g[u] {
243-
t := dfs(v)
244-
size += t
245-
score *= t
246-
}
247-
if u > 0 {
248-
score *= n - size
249-
}
250-
if score > maxScore {
251-
maxScore, ans = score, 1
252-
} else if score == maxScore {
253-
ans++
254-
}
255-
return size
256-
}
257-
dfs(0)
258-
return ans
321+
return cnt;
322+
}
259323
}
260324
```
261325

0 commit comments

Comments
 (0)