-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2872-maximum-number-of-k-divisible-components.js
More file actions
51 lines (48 loc) · 1.5 KB
/
2872-maximum-number-of-k-divisible-components.js
File metadata and controls
51 lines (48 loc) · 1.5 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
/**
* 2872. Maximum Number of K-Divisible Components
* https://leetcode.com/problems/maximum-number-of-k-divisible-components/
* Difficulty: Hard
*
* There is an undirected tree with n nodes labeled from 0 to n - 1. You are given the integer
* n and 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, and an integer k.
*
* A valid split of the tree is obtained by removing any set of edges, possibly empty, from the
* tree such that the resulting components all have values that are divisible by k, where the
* value of a connected component is the sum of the values of its nodes.
*
* Return the maximum number of components in any valid split.
*/
/**
* @param {number} n
* @param {number[][]} edges
* @param {number[]} values
* @param {number} k
* @return {number}
*/
var maxKDivisibleComponents = function(n, edges, values, k) {
const graph = Array.from({ length: n }, () => []);
for (const [u, v] of edges) {
graph[u].push(v);
graph[v].push(u);
}
let result = 0;
dfs(0, -1);
return result;
function dfs(node, parent) {
let total = values[node];
for (const neighbor of graph[node]) {
if (neighbor !== parent) {
total += dfs(neighbor, node);
}
}
if (total % k === 0) {
result++;
return 0;
}
return total;
}
};