diff --git a/leetcode/2001-2100/2049.Count-Nodes-With-the-Highest-Score/1.png b/leetcode/2001-2100/2049.Count-Nodes-With-the-Highest-Score/1.png new file mode 100644 index 000000000..43805c888 Binary files /dev/null and b/leetcode/2001-2100/2049.Count-Nodes-With-the-Highest-Score/1.png differ diff --git a/leetcode/2001-2100/2049.Count-Nodes-With-the-Highest-Score/2.png b/leetcode/2001-2100/2049.Count-Nodes-With-the-Highest-Score/2.png new file mode 100644 index 000000000..1bce94398 Binary files /dev/null and b/leetcode/2001-2100/2049.Count-Nodes-With-the-Highest-Score/2.png differ diff --git a/leetcode/2001-2100/2049.Count-Nodes-With-the-Highest-Score/README.md b/leetcode/2001-2100/2049.Count-Nodes-With-the-Highest-Score/README.md index 8550b108f..1918d064a 100755 --- a/leetcode/2001-2100/2049.Count-Nodes-With-the-Highest-Score/README.md +++ b/leetcode/2001-2100/2049.Count-Nodes-With-the-Highest-Score/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/2001-2100/2049.Count-Nodes-With-the-Highest-Score/Solution.go b/leetcode/2001-2100/2049.Count-Nodes-With-the-Highest-Score/Solution.go index d115ccf5e..bbbd64618 100644 --- a/leetcode/2001-2100/2049.Count-Nodes-With-the-Highest-Score/Solution.go +++ b/leetcode/2001-2100/2049.Count-Nodes-With-the-Highest-Score/Solution.go @@ -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 } diff --git a/leetcode/2001-2100/2049.Count-Nodes-With-the-Highest-Score/Solution_test.go b/leetcode/2001-2100/2049.Count-Nodes-With-the-Highest-Score/Solution_test.go index 14ff50eb4..75a4ce3ed 100644 --- a/leetcode/2001-2100/2049.Count-Nodes-With-the-Highest-Score/Solution_test.go +++ b/leetcode/2001-2100/2049.Count-Nodes-With-the-Highest-Score/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }