Skip to content

Commit 06ac46a

Browse files
committed
Add solution and test-cases for problem 3372
1 parent 31756cd commit 06ac46a

File tree

5 files changed

+99
-26
lines changed

5 files changed

+99
-26
lines changed
Loading
Loading

leetcode/3301-3400/3372.Maximize-the-Number-of-Target-Nodes-After-Connecting-Trees-I/README.md

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
11
# [3372.Maximize the Number of Target Nodes After Connecting Trees I][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
There exist two **undirected** trees with `n` and `m` nodes, with **distinct** labels in ranges `[0, n - 1]` and `[0, m - 1]`, respectively.
5+
6+
You are given two 2D integer arrays `edges1` and `edges2` of lengths `n - 1` and `m - 1`, respectively, where `edges1[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi` in the first tree and `edges2[i] = [ui, vi]` indicates that there is an edge between nodes `ui` and `vi` in the second tree. You are also given an integer `k`.
7+
8+
Node `u` is **target** to node `v` if the number of edges on the path from `u` to `v` is less than or equal to `k`. **Note** that a node is always **target** to itself.
9+
10+
Return an array of n integers `answer`, where `answer[i]` is the **maximum** possible number of nodes **target** to node `i` of the first tree if you have to connect one node from the first tree to another node in the second tree.
11+
12+
Note that queries are independent from each other. That is, for every query you will remove the added edge before proceeding to the next query.
13+
14+
**Example 1:**
715

8-
**Example 1:**
16+
![1](./1.png)
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: edges1 = [[0,1],[0,2],[2,3],[2,4]], edges2 = [[0,1],[0,2],[0,3],[2,7],[1,4],[4,5],[4,6]], k = 2
20+
21+
Output: [9,7,9,8,8]
22+
23+
Explanation:
24+
25+
For i = 0, connect node 0 from the first tree to node 0 from the second tree.
26+
For i = 1, connect node 1 from the first tree to node 0 from the second tree.
27+
For i = 2, connect node 2 from the first tree to node 4 from the second tree.
28+
For i = 3, connect node 3 from the first tree to node 4 from the second tree.
29+
For i = 4, connect node 4 from the first tree to node 4 from the second tree.
1330
```
1431

15-
## 题意
16-
> ...
32+
**Example 2:**
1733

18-
## 题解
34+
![2](./2.png)
1935

20-
### 思路1
21-
> ...
22-
Maximize the Number of Target Nodes After Connecting Trees I
23-
```go
2436
```
37+
Input: edges1 = [[0,1],[0,2],[0,3],[0,4]], edges2 = [[0,1],[1,2],[2,3]], k = 1
2538
39+
Output: [6,3,3,3,3]
40+
41+
Explanation:
42+
43+
For every i, connect node i of the first tree with any node of the second tree.
44+
```
2645

2746
## 结语
2847

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,59 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "slices"
4+
5+
func dfs3372(adj map[int][]int, start, parent, limit int) int {
6+
if limit < 0 {
7+
return 0
8+
}
9+
10+
cnt := 1 // self
11+
for _, rel := range adj[start] {
12+
if rel == parent {
13+
continue
14+
}
15+
cnt += dfs3372(adj, rel, start, limit-1)
16+
}
17+
return cnt
18+
19+
}
20+
21+
func calDistance(adj map[int][]int, n, limit int) []int {
22+
distance := make([]int, n)
23+
for i := 0; i < n; i++ {
24+
distance[i] = dfs3372(adj, i, -1, limit)
25+
}
26+
return distance
27+
}
28+
29+
func Solution(edges1 [][]int, edges2 [][]int, k int) []int {
30+
n, m := 0, 0
31+
for _, e := range edges1 {
32+
n = max(n, e[0], e[1])
33+
}
34+
for _, e := range edges2 {
35+
m = max(m, e[0], e[1])
36+
}
37+
n, m = n+1, m+1
38+
adj1 := make(map[int][]int)
39+
adj2 := make(map[int][]int)
40+
41+
for _, e := range edges1 {
42+
adj1[e[0]] = append(adj1[e[0]], e[1])
43+
adj1[e[1]] = append(adj1[e[1]], e[0])
44+
}
45+
for _, e := range edges2 {
46+
adj2[e[0]] = append(adj2[e[0]], e[1])
47+
adj2[e[1]] = append(adj2[e[1]], e[0])
48+
}
49+
50+
dis1 := calDistance(adj1, n, k)
51+
dis2 := calDistance(adj2, m, k-1)
52+
53+
cnt := slices.Max(dis2)
54+
for i := range n {
55+
dis1[i] += cnt
56+
}
57+
58+
return dis1
559
}

leetcode/3301-3400/3372.Maximize-the-Number-of-Target-Nodes-After-Connecting-Trees-I/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
edges1, edges2 [][]int
14+
k int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", [][]int{{0, 1}, {0, 2}, {2, 3}, {2, 4}}, [][]int{{0, 1}, {0, 2}, {0, 3}, {2, 7}, {1, 4}, {1, 5}, {4, 5}, {4, 6}}, 2, []int{9, 7, 9, 8, 8}},
18+
{"TestCase2", [][]int{{0, 1}, {0, 2}, {0, 3}, {0, 4}}, [][]int{{0, 1}, {1, 2}, {2, 3}}, 1, []int{6, 3, 3, 3, 3}},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.edges1, c.edges2, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
27+
c.expect, got, c.edges1, c.edges2, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)