Skip to content

Add solution and test-cases for problem 2049 #1228

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
# [2049.Count Nodes With the Highest Score][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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`.

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.

Return the **number** of nodes that have the **highest score**.

**Example 1:**
**Example 1:**

![1](./1.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: parents = [-1,2,0,2,0]
Output: 3
Explanation:
- The score of node 0 is: 3 * 1 = 3
- The score of node 1 is: 4 = 4
- The score of node 2 is: 1 * 1 * 2 = 2
- The score of node 3 is: 4 = 4
- The score of node 4 is: 4 = 4
The highest score is 4, and three nodes (node 1, node 3, and node 4) have the highest score.
```

## 题意
> ...
**Example 2:**

## 题解
![2](./2.png)

### 思路1
> ...
Count Nodes With the Highest Score
```go
```

Input: parents = [-1,2,0]
Output: 2
Explanation:
- The score of node 0 is: 2 = 2
- The score of node 1 is: 2 = 2
- The score of node 2 is: 1 * 1 = 1
The highest score is 2, and two nodes (node 0 and node 1) have the highest score.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(parents []int) int {
adj := make(map[int][]int)
for c, f := range parents {
adj[f] = append(adj[f], c)
}
n := len(parents)
children := make([][2]int, n)
var dfs func(int) int
dfs = func(root int) int {
children[root] = [2]int{-1, -1}
index := 0
count := 0
for _, child := range adj[root] {
children[root][index] = dfs(child)
count += children[root][index]
index++
}
return count + 1
}
_ = dfs(0)
ans := 0
// 尝试移除0
left := children[0][0]
if left == -1 {
left = 1
}
right := children[0][1]
if right == -1 {
right = 1
}
ans = left * right
count := 1
for i := 1; i < n; i++ {
x := n
x--
left = children[i][0]
right = children[i][1]
if left != -1 {
x -= left
} else {
left = 1
}
if right != -1 {
x -= right
} else {
right = 1
}
now := left * right * x
if now == ans {
count++
} else if now > ans {
ans = now
count = 1
}
}
return count
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{-1, 2, 0, 2, 0}, 3},
{"TestCase2", []int{-1, 2, 0}, 2},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading