LeetCode 2049. Count Nodes With the Highest Score
原题链接在这里:https://leetcode.com/problems/count-nodes-with-the-highest-score/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:
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:
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.
Constraints:
n == parents.length
2 <= n <= 105
parents[0] == -1
0 <= parents[i] <= n - 1
fori != 0
parents
represents a valid binary tree.
题解:
For each node, there are at most 3 edges, left, right, and up. Product is to multiply the nodes in the 3 components.
Then we need to know how many nodes are in each component, we could count the nodes using DFS bottom up.
For up component, its count = n - leftCount - rightCount - 1. But for root, it could be 0, we need make sure it is at least 1.
And keep updating the maximum product and result.
Time Complexity: O(n). n = parents.length.
Space: O(n).
AC Java:
1 class Solution { 2 int res = 0; 3 long max = 1; 4 public int countHighestScoreNodes(int[] parents) { 5 int n = parents.length; 6 List<Integer>[] graph = new ArrayList[n]; 7 for(int i = 0; i < n; i++){ 8 graph[i] = new ArrayList<>(); 9 } 10 11 for(int i = 1; i < n; i++){ 12 graph[parents[i]].add(i); 13 } 14 15 dfs(graph, 0, n); 16 return res; 17 } 18 19 private int dfs(List<Integer>[] graph, int node, int n){ 20 int total = 0; 21 long product = 1L; 22 for(int next : graph[node]){ 23 int count = dfs(graph, next, n); 24 total += count; 25 product *= count; 26 } 27 28 product *= Math.max(1, n - total - 1); 29 if(product > max){ 30 max = product; 31 res = 1; 32 }else if(product == max){ 33 res++; 34 } 35 36 return total + 1; 37 } 38 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2019-08-14 LeetCode 1059. All Paths from Source Lead to Destination