Skip to content

Commit 67ed7ad

Browse files
committed
Add solution and test-cases for problem 2872
1 parent dbd4dda commit 67ed7ad

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# [2872.Maximum Number of K-Divisible Components][title]
2+
3+
## Description
4+
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.
5+
6+
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`.
7+
8+
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.
9+
10+
Return the **maximum number of components** in any valid split.
11+
12+
13+
**Example 1:**
14+
15+
```
16+
Input: n = 5, edges = [[0,2],[1,2],[1,3],[2,4]], values = [1,8,1,4,4], k = 6
17+
Output: 2
18+
Explanation: We remove the edge connecting node 1 with 2. The resulting split is valid because:
19+
- The value of the component containing nodes 1 and 3 is values[1] + values[3] = 12.
20+
- The value of the component containing nodes 0, 2, and 4 is values[0] + values[2] + values[4] = 6.
21+
It can be shown that no other valid split has more than 2 connected components.
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: n = 7, edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [3,0,6,1,5,2,1], k = 3
28+
Output: 3
29+
Explanation: We remove the edge connecting node 0 with 2, and the edge connecting node 0 with 1. The resulting split is valid because:
30+
- The value of the component containing node 0 is values[0] = 3.
31+
- The value of the component containing nodes 2, 5, and 6 is values[2] + values[5] + values[6] = 9.
32+
- The value of the component containing nodes 1, 3, and 4 is values[1] + values[3] + values[4] = 6.
33+
It can be shown that no other valid split has more than 3 connected components.
34+
```
35+
36+
## 结语
37+
38+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
39+
40+
[title]: https://leetcode.com/problems/maximum-number-of-k-divisible-components/
41+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package Solution
2+
3+
func Solution(n int, edges [][]int, values []int, k int) int {
4+
for i := range values {
5+
values[i] %= k
6+
}
7+
adj := make([][]int, n)
8+
for _, e := range edges {
9+
a, b := e[0], e[1]
10+
adj[a] = append(adj[a], b)
11+
adj[b] = append(adj[b], a)
12+
}
13+
14+
count := 0
15+
var dfs func(int, int) int
16+
dfs = func(parent, cur int) int {
17+
s := 0
18+
for _, nei := range adj[cur] {
19+
if nei == parent {
20+
continue
21+
}
22+
s = (s + dfs(cur, nei)) % k
23+
}
24+
s = (s + values[cur]) % k
25+
if s == 0 {
26+
count++
27+
}
28+
return s
29+
}
30+
dfs(-1, 0)
31+
return count
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"strconv"
6+
"testing"
7+
)
8+
9+
func TestSolution(t *testing.T) {
10+
// 测试用例
11+
cases := []struct {
12+
name string
13+
n int
14+
edges [][]int
15+
values []int
16+
k int
17+
expect int
18+
}{
19+
{"TestCase1", 5, [][]int{{0, 2}, {1, 2}, {1, 3}, {2, 4}}, []int{1, 8, 1, 4, 4}, 6, 2},
20+
{"TestCase2", 7, [][]int{{0, 1}, {0, 2}, {1, 3}, {1, 4}, {2, 5}, {2, 6}}, []int{3, 0, 6, 1, 5, 2, 1}, 3, 3},
21+
}
22+
23+
// 开始测试
24+
for i, c := range cases {
25+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
26+
got := Solution(c.n, c.edges, c.values, c.k)
27+
if !reflect.DeepEqual(got, c.expect) {
28+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v %v",
29+
c.expect, got, c.n, c.edges, c.values, c.k)
30+
}
31+
})
32+
}
33+
}
34+
35+
// 压力测试
36+
func BenchmarkSolution(b *testing.B) {
37+
}
38+
39+
// 使用案列
40+
func ExampleSolution() {
41+
}

0 commit comments

Comments
 (0)