Skip to content

Commit bcba20b

Browse files
committed
Add solution and test-cases for problem 2368
1 parent 9f83439 commit bcba20b

File tree

5 files changed

+65
-27
lines changed

5 files changed

+65
-27
lines changed
Loading
Loading

leetcode/2301-2400/2368.Reachable-Nodes-With-Restrictions/README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
# [2368.Reachable Nodes With Restrictions][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 is an undirected tree with `n` nodes labeled from `0` to `n - 1` and `n - 1` edges.
5+
6+
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 an integer array `restricted` which represents **restricted** nodes.
7+
8+
Return the **maximum** number of nodes you can reach from node 0 without visiting a restricted node.
9+
10+
Note that node `0` will **not** be a restricted node.
711

8-
**Example 1:**
12+
**Example 1:**
13+
14+
![1](./1.png)
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
18+
Output: 4
19+
Explanation: The diagram above shows the tree.
20+
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
1321
```
1422

15-
## 题意
16-
> ...
23+
**Example 2:**
1724

18-
## 题解
25+
![2](./2.png)
1926

20-
### 思路1
21-
> ...
22-
Reachable Nodes With Restrictions
23-
```go
2427
```
25-
28+
Input: n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
29+
Output: 3
30+
Explanation: The diagram above shows the tree.
31+
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
32+
```
2633

2734
## 结语
2835

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int, edges [][]int, restricted []int) int {
4+
restrictedMap := make(map[int]struct{})
5+
for _, n := range restricted {
6+
restrictedMap[n] = struct{}{}
7+
}
8+
adj := make([][]int, n)
9+
for _, e := range edges {
10+
adj[e[0]] = append(adj[e[0]], e[1])
11+
adj[e[1]] = append(adj[e[1]], e[0])
12+
}
13+
ans := 0
14+
q := []int{0}
15+
v := make([]bool, n)
16+
v[0] = true
17+
for len(q) > 0 {
18+
nq := make([]int, 0)
19+
ans += len(q)
20+
for _, cur := range q {
21+
for _, next := range adj[cur] {
22+
if v[next] {
23+
continue
24+
}
25+
if _, ok := restrictedMap[next]; ok {
26+
continue
27+
}
28+
nq = append(nq, next)
29+
v[next] = true
30+
}
31+
}
32+
q = nq
33+
}
34+
return ans
535
}

leetcode/2301-2400/2368.Reachable-Nodes-With-Restrictions/Solution_test.go

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

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.n, c.edges, c.restricted)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
28+
c.expect, got, c.n, c.edges, c.restricted)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)