Skip to content

Commit

Permalink
Merge pull request #1070 from 0xff-dev/2872
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 2872
  • Loading branch information
6boris authored Dec 24, 2024
2 parents 69fb488 + 67ed7ad commit a233a34
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# [2872.Maximum Number of K-Divisible Components][title]

## Description
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.


**Example 1:**

```
Input: n = 5, edges = [[0,2],[1,2],[1,3],[2,4]], values = [1,8,1,4,4], k = 6
Output: 2
Explanation: We remove the edge connecting node 1 with 2. The resulting split is valid because:
- The value of the component containing nodes 1 and 3 is values[1] + values[3] = 12.
- The value of the component containing nodes 0, 2, and 4 is values[0] + values[2] + values[4] = 6.
It can be shown that no other valid split has more than 2 connected components.
```

**Example 2:**

```
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
Output: 3
Explanation: We remove the edge connecting node 0 with 2, and the edge connecting node 0 with 1. The resulting split is valid because:
- The value of the component containing node 0 is values[0] = 3.
- The value of the component containing nodes 2, 5, and 6 is values[2] + values[5] + values[6] = 9.
- The value of the component containing nodes 1, 3, and 4 is values[1] + values[3] + values[4] = 6.
It can be shown that no other valid split has more than 3 connected components.
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/maximum-number-of-k-divisible-components/
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package Solution

func Solution(n int, edges [][]int, values []int, k int) int {
for i := range values {
values[i] %= k
}
adj := make([][]int, n)
for _, e := range edges {
a, b := e[0], e[1]
adj[a] = append(adj[a], b)
adj[b] = append(adj[b], a)
}

count := 0
var dfs func(int, int) int
dfs = func(parent, cur int) int {
s := 0
for _, nei := range adj[cur] {
if nei == parent {
continue
}
s = (s + dfs(cur, nei)) % k
}
s = (s + values[cur]) % k
if s == 0 {
count++
}
return s
}
dfs(-1, 0)
return count
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package Solution

import (
"reflect"
"strconv"
"testing"
)

func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
n int
edges [][]int
values []int
k int
expect int
}{
{"TestCase1", 5, [][]int{{0, 2}, {1, 2}, {1, 3}, {2, 4}}, []int{1, 8, 1, 4, 4}, 6, 2},
{"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},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.n, c.edges, c.values, c.k)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v %v",
c.expect, got, c.n, c.edges, c.values, c.k)
}
})
}
}

// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
func ExampleSolution() {
}

0 comments on commit a233a34

Please sign in to comment.