Skip to content

Commit

Permalink
Add solution and test-cases for problem 2368
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Jul 29, 2024
1 parent 9f83439 commit bcba20b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 27 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 21 additions & 14 deletions leetcode/2301-2400/2368.Reachable-Nodes-With-Restrictions/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# [2368.Reachable Nodes With Restrictions][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
There is an undirected tree with `n` nodes labeled from `0` to `n - 1` and `n - 1` edges.

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.

Return the **maximum** number of nodes you can reach from node 0 without visiting a restricted node.

Note that node `0` will **not** be a restricted node.

**Example 1:**
**Example 1:**

![1](./1.png)

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

## 题意
> ...
**Example 2:**

## 题解
![2](./2.png)

### 思路1
> ...
Reachable Nodes With Restrictions
```go
```

Input: n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
Output: 3
Explanation: The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(n int, edges [][]int, restricted []int) int {
restrictedMap := make(map[int]struct{})
for _, n := range restricted {
restrictedMap[n] = struct{}{}
}
adj := make([][]int, n)
for _, e := range edges {
adj[e[0]] = append(adj[e[0]], e[1])
adj[e[1]] = append(adj[e[1]], e[0])
}
ans := 0
q := []int{0}
v := make([]bool, n)
v[0] = true
for len(q) > 0 {
nq := make([]int, 0)
ans += len(q)
for _, cur := range q {
for _, next := range adj[cur] {
if v[next] {
continue
}
if _, ok := restrictedMap[next]; ok {
continue
}
nq = append(nq, next)
v[next] = true
}
}
q = nq
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
n int
edges [][]int
restricted []int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 7, [][]int{{0, 1}, {1, 2}, {3, 1}, {4, 0}, {0, 5}, {5, 6}}, []int{4, 5}, 4},
{"TestCase", 7, [][]int{{0, 1}, {0, 2}, {0, 5}, {0, 4}, {3, 2}, {6, 5}}, []int{4, 2, 1}, 3},
}

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

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

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

0 comments on commit bcba20b

Please sign in to comment.