diff --git a/leetcode/3301-3400/3310.Remove-Methods-From-Project/1.png b/leetcode/3301-3400/3310.Remove-Methods-From-Project/1.png new file mode 100644 index 00000000..7e2c2497 Binary files /dev/null and b/leetcode/3301-3400/3310.Remove-Methods-From-Project/1.png differ diff --git a/leetcode/3301-3400/3310.Remove-Methods-From-Project/2.png b/leetcode/3301-3400/3310.Remove-Methods-From-Project/2.png new file mode 100644 index 00000000..e9e206c0 Binary files /dev/null and b/leetcode/3301-3400/3310.Remove-Methods-From-Project/2.png differ diff --git a/leetcode/3301-3400/3310.Remove-Methods-From-Project/3.png b/leetcode/3301-3400/3310.Remove-Methods-From-Project/3.png new file mode 100644 index 00000000..b9056067 Binary files /dev/null and b/leetcode/3301-3400/3310.Remove-Methods-From-Project/3.png differ diff --git a/leetcode/3301-3400/3310.Remove-Methods-From-Project/README.md b/leetcode/3301-3400/3310.Remove-Methods-From-Project/README.md index 4ae8edce..08711fe6 100755 --- a/leetcode/3301-3400/3310.Remove-Methods-From-Project/README.md +++ b/leetcode/3301-3400/3310.Remove-Methods-From-Project/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/3301-3400/3310.Remove-Methods-From-Project/Solution.go b/leetcode/3301-3400/3310.Remove-Methods-From-Project/Solution.go index d115ccf5..2bd87966 100644 --- a/leetcode/3301-3400/3310.Remove-Methods-From-Project/Solution.go +++ b/leetcode/3301-3400/3310.Remove-Methods-From-Project/Solution.go @@ -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 } diff --git a/leetcode/3301-3400/3310.Remove-Methods-From-Project/Solution_test.go b/leetcode/3301-3400/3310.Remove-Methods-From-Project/Solution_test.go index 14ff50eb..f41189d5 100644 --- a/leetcode/3301-3400/3310.Remove-Methods-From-Project/Solution_test.go +++ b/leetcode/3301-3400/3310.Remove-Methods-From-Project/Solution_test.go @@ -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() { }