Skip to content

Add solution and test-cases for problem 3310 #1232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 42 additions & 13 deletions leetcode/3301-3400/3310.Remove-Methods-From-Project/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,57 @@
# [3310.Remove Methods From Project][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
You are maintaining a project that has `n` methods numbered from `0` to `n - 1`.

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`.

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.

A group of methods can only be removed if no method **outside** the group invokes any methods **within** it.

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.

**Example 1:**

![1](./1.png)

```
Input: n = 4, k = 1, invocations = [[1,2],[0,1],[3,2]]

Output: [0,1,2,3]

**Example 1:**
Explanation:

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.
```
Input: a = "11", b = "1"
Output: "100"

**Example 2:**

![2](./2.png)

```
Input: n = 5, k = 0, invocations = [[1,2],[0,2],[0,1],[3,4]]

Output: [3,4]

Explanation:

Methods 0, 1, and 2 are suspicious and they are not directly invoked by any other method. We can remove them.
```

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

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

### 思路1
> ...
Remove Methods From Project
```go
```
Input: n = 3, k = 2, invocations = [[1,2],[0,1],[2,0]]

Output: []

Explanation:

All methods are suspicious. We can remove them.
```

## 结语

Expand Down
70 changes: 68 additions & 2 deletions leetcode/3301-3400/3310.Remove-Methods-From-Project/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,71 @@
package Solution

func Solution(x bool) bool {
return x
type unionFind3310 struct {
father []int
}

func (u *unionFind3310) findFather(x int) int {
if x != u.father[x] {
u.father[x] = u.findFather(u.father[x])
}
return u.father[x]
}

func (u *unionFind3310) union(x, y int) {
fx := u.findFather(x)
fy := u.findFather(y)
if fx < fy {
u.father[fy] = fx
} else {
u.father[fx] = fy
}
}

func Solution(n int, k int, invocations [][]int) []int {
u := unionFind3310{father: make([]int, n)}
for i := range n {
u.father[i] = i
}
relations := make(map[int][]int)
removed := make([]bool, n)
for _, in := range invocations {
u.union(in[0], in[1])
relations[in[0]] = append(relations[in[0]], in[1])
}
// 然后通过dfs将所有的问题方法都标记出来
// 这个组的数据是有问题的
problemGroup := u.findFather(k)
// 将所有的数据开始做标记
removed[k] = true
var dfs func(int)
used := map[int]bool{
k: true,
}
dfs = func(cur int) {
removed[cur] = true
for _, rel := range relations[cur] {
if used[rel] {
continue
}
used[rel] = true
dfs(rel)
}
}
dfs(k)
var res, keep []int

for i := range n {
f := u.findFather(i)
if f != problemGroup {
res = append(res, i)
continue
}
if !removed[i] {
for j := range n {
keep = append(keep, j)
}
return keep
}
}
return res
}
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, k int
invocations [][]int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 4, 1, [][]int{{1, 2}, {0, 1}, {3, 2}}, []int{0, 1, 2, 3}},
{"TestCase2", 5, 0, [][]int{{1, 2}, {0, 2}, {0, 1}, {3, 4}}, []int{3, 4}},
{"TestCase3", 3, 2, [][]int{{1, 2}, {0, 1}, {2, 0}}, nil},
}

// 开始测试
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.k, c.invocations)
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.k, c.invocations)
}
})
}
}

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

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