Skip to content

Commit 5c05458

Browse files
committed
Add solution and test-cases for problem 2049
1 parent 31756cd commit 5c05458

File tree

5 files changed

+89
-23
lines changed

5 files changed

+89
-23
lines changed
Loading
Loading

leetcode/2001-2100/2049.Count-Nodes-With-the-Highest-Score/README.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [2049.Count Nodes With the Highest Score][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
There is a **binary** tree rooted at `0` consisting of `n` nodes. The nodes are labeled from `0` to `n - 1`. You are given a **0-indexed** integer array `parents` representing the tree, where `parents[i]` is the parent of node `i`. Since node `0` is the root, `parents[0] == -1`.
5+
6+
Each node has a **score**. To find the score of a node, consider if the node and the edges connected to it were **removed**. The tree would become one or more **non-empty** subtrees. The **size** of a subtree is the number of the nodes in it. The **score** of the node is the **product of the sizes** of all those subtrees.
7+
8+
Return the **number** of nodes that have the **highest score**.
79

8-
**Example 1:**
10+
**Example 1:**
11+
12+
![1](./1.png)
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: parents = [-1,2,0,2,0]
16+
Output: 3
17+
Explanation:
18+
- The score of node 0 is: 3 * 1 = 3
19+
- The score of node 1 is: 4 = 4
20+
- The score of node 2 is: 1 * 1 * 2 = 2
21+
- The score of node 3 is: 4 = 4
22+
- The score of node 4 is: 4 = 4
23+
The highest score is 4, and three nodes (node 1, node 3, and node 4) have the highest score.
1324
```
1425

15-
## 题意
16-
> ...
26+
**Example 2:**
1727

18-
## 题解
28+
![2](./2.png)
1929

20-
### 思路1
21-
> ...
22-
Count Nodes With the Highest Score
23-
```go
2430
```
25-
31+
Input: parents = [-1,2,0]
32+
Output: 2
33+
Explanation:
34+
- The score of node 0 is: 2 = 2
35+
- The score of node 1 is: 2 = 2
36+
- The score of node 2 is: 1 * 1 = 1
37+
The highest score is 2, and two nodes (node 0 and node 1) have the highest score.
38+
```
2639

2740
## 结语
2841

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,59 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(parents []int) int {
4+
adj := make(map[int][]int)
5+
for c, f := range parents {
6+
adj[f] = append(adj[f], c)
7+
}
8+
n := len(parents)
9+
children := make([][2]int, n)
10+
var dfs func(int) int
11+
dfs = func(root int) int {
12+
children[root] = [2]int{-1, -1}
13+
index := 0
14+
count := 0
15+
for _, child := range adj[root] {
16+
children[root][index] = dfs(child)
17+
count += children[root][index]
18+
index++
19+
}
20+
return count + 1
21+
}
22+
_ = dfs(0)
23+
ans := 0
24+
// 尝试移除0
25+
left := children[0][0]
26+
if left == -1 {
27+
left = 1
28+
}
29+
right := children[0][1]
30+
if right == -1 {
31+
right = 1
32+
}
33+
ans = left * right
34+
count := 1
35+
for i := 1; i < n; i++ {
36+
x := n
37+
x--
38+
left = children[i][0]
39+
right = children[i][1]
40+
if left != -1 {
41+
x -= left
42+
} else {
43+
left = 1
44+
}
45+
if right != -1 {
46+
x -= right
47+
} else {
48+
right = 1
49+
}
50+
now := left * right * x
51+
if now == ans {
52+
count++
53+
} else if now > ans {
54+
ans = now
55+
count = 1
56+
}
57+
}
58+
return count
559
}

leetcode/2001-2100/2049.Count-Nodes-With-the-Highest-Score/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{-1, 2, 0, 2, 0}, 3},
17+
{"TestCase2", []int{-1, 2, 0}, 2},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)