-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2925-maximum-score-after-applying-operations-on-a-tree.js
More file actions
58 lines (50 loc) · 1.68 KB
/
2925-maximum-score-after-applying-operations-on-a-tree.js
File metadata and controls
58 lines (50 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* 2925. Maximum Score After Applying Operations on a Tree
* https://leetcode.com/problems/maximum-score-after-applying-operations-on-a-tree/
* Difficulty: Medium
*
* There is an undirected tree with n nodes labeled from 0 to n - 1, and rooted at node 0. You are
* given a 2D integer array edges of length n - 1, where edges[i] = [ai, bi] indicates that there
* is an edge between nodes ai and bi in the tree.
*
* You are also given a 0-indexed integer array values of length n, where values[i] is the value
* associated with the ith node.
*
* You start with a score of 0. In one operation, you can:
* - Pick any node i.
* - Add values[i] to your score.
* - Set values[i] to 0.
*
* A tree is healthy if the sum of values on the path from the root to any leaf node is different
* than zero.
*
* Return the maximum score you can obtain after performing these operations on the tree any
* number of times so that it remains healthy.
*/
/**
* @param {number[][]} edges
* @param {number[]} values
* @return {number}
*/
var maximumScoreAfterOperations = function(edges, values) {
const n = values.length;
const graph = new Array(n).fill().map(() => []);
for (const [u, v] of edges) {
graph[u].push(v);
graph[v].push(u);
}
const totalSum = values.reduce((sum, val) => sum + val, 0);
const minToKeep = dfs(0, -1);
return totalSum - minToKeep;
function dfs(node, parent) {
const children = graph[node].filter(child => child !== parent);
if (children.length === 0) {
return values[node];
}
let childrenSum = 0;
for (const child of children) {
childrenSum += dfs(child, node);
}
return Math.min(values[node], childrenSum);
}
};