Skip to content

Commit a5c38f4

Browse files
authored
Merge pull request #1232 from 0xff-dev/3310
Add solution and test-cases for problem 3310
2 parents 469cd25 + ca1ceef commit a5c38f4

File tree

6 files changed

+122
-26
lines changed

6 files changed

+122
-26
lines changed
12 KB
Loading
12.9 KB
Loading
10.5 KB
Loading

leetcode/3301-3400/3310.Remove-Methods-From-Project/README.md

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,57 @@
11
# [3310.Remove Methods From Project][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+
You are maintaining a project that has `n` methods numbered from `0` to `n - 1`.
5+
6+
You are given two integers `n` and `k`, and a 2D integer array `invocations`, where `invocations[i] = [ai, bi]` indicates that method `ai` invokes method `bi`.
7+
8+
There is a known bug in method `k`. Method `k`, along with any method invoked by it, either **directly** or **indirectly**, are considered **suspicious** and we aim to remove them.
9+
10+
A group of methods can only be removed if no method **outside** the group invokes any methods **within** it.
11+
12+
Return an array containing all the remaining methods after removing all the **suspicious** methods. You may return the answer in any order. If it is not possible to remove **all** the suspicious methods, **none** should be removed.
13+
14+
**Example 1:**
15+
16+
![1](./1.png)
17+
18+
```
19+
Input: n = 4, k = 1, invocations = [[1,2],[0,1],[3,2]]
20+
21+
Output: [0,1,2,3]
722
8-
**Example 1:**
23+
Explanation:
924
25+
Method 2 and method 1 are suspicious, but they are directly invoked by methods 3 and 0, which are not suspicious. We return all elements without removing anything.
1026
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
27+
28+
**Example 2:**
29+
30+
![2](./2.png)
31+
32+
```
33+
Input: n = 5, k = 0, invocations = [[1,2],[0,2],[0,1],[3,4]]
34+
35+
Output: [3,4]
36+
37+
Explanation:
38+
39+
Methods 0, 1, and 2 are suspicious and they are not directly invoked by any other method. We can remove them.
1340
```
1441

15-
## 题意
16-
> ...
42+
**Example 3:**
1743

18-
## 题解
44+
![3](./3.png)
1945

20-
### 思路1
21-
> ...
22-
Remove Methods From Project
23-
```go
2446
```
47+
Input: n = 3, k = 2, invocations = [[1,2],[0,1],[2,0]]
48+
49+
Output: []
2550
51+
Explanation:
52+
53+
All methods are suspicious. We can remove them.
54+
```
2655

2756
## 结语
2857

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

3-
func Solution(x bool) bool {
4-
return x
3+
type unionFind3310 struct {
4+
father []int
5+
}
6+
7+
func (u *unionFind3310) findFather(x int) int {
8+
if x != u.father[x] {
9+
u.father[x] = u.findFather(u.father[x])
10+
}
11+
return u.father[x]
12+
}
13+
14+
func (u *unionFind3310) union(x, y int) {
15+
fx := u.findFather(x)
16+
fy := u.findFather(y)
17+
if fx < fy {
18+
u.father[fy] = fx
19+
} else {
20+
u.father[fx] = fy
21+
}
22+
}
23+
24+
func Solution(n int, k int, invocations [][]int) []int {
25+
u := unionFind3310{father: make([]int, n)}
26+
for i := range n {
27+
u.father[i] = i
28+
}
29+
relations := make(map[int][]int)
30+
removed := make([]bool, n)
31+
for _, in := range invocations {
32+
u.union(in[0], in[1])
33+
relations[in[0]] = append(relations[in[0]], in[1])
34+
}
35+
// 然后通过dfs将所有的问题方法都标记出来
36+
// 这个组的数据是有问题的
37+
problemGroup := u.findFather(k)
38+
// 将所有的数据开始做标记
39+
removed[k] = true
40+
var dfs func(int)
41+
used := map[int]bool{
42+
k: true,
43+
}
44+
dfs = func(cur int) {
45+
removed[cur] = true
46+
for _, rel := range relations[cur] {
47+
if used[rel] {
48+
continue
49+
}
50+
used[rel] = true
51+
dfs(rel)
52+
}
53+
}
54+
dfs(k)
55+
var res, keep []int
56+
57+
for i := range n {
58+
f := u.findFather(i)
59+
if f != problemGroup {
60+
res = append(res, i)
61+
continue
62+
}
63+
if !removed[i] {
64+
for j := range n {
65+
keep = append(keep, j)
66+
}
67+
return keep
68+
}
69+
}
70+
return res
571
}

leetcode/3301-3400/3310.Remove-Methods-From-Project/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, k int
14+
invocations [][]int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 4, 1, [][]int{{1, 2}, {0, 1}, {3, 2}}, []int{0, 1, 2, 3}},
18+
{"TestCase2", 5, 0, [][]int{{1, 2}, {0, 2}, {0, 1}, {3, 4}}, []int{3, 4}},
19+
{"TestCase3", 3, 2, [][]int{{1, 2}, {0, 1}, {2, 0}}, nil},
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.k, c.invocations)
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.k, c.invocations)
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)