|
| 1 | +[](https://github.com/javadev/LeetCode-in-Kotlin) |
| 2 | +[](https://github.com/javadev/LeetCode-in-Kotlin/fork) |
| 3 | + |
| 4 | +## 3310\. Remove Methods From Project |
| 5 | + |
| 6 | +Medium |
| 7 | + |
| 8 | +You are maintaining a project that has `n` methods numbered from `0` to `n - 1`. |
| 9 | + |
| 10 | +You are given two integers `n` and `k`, and a 2D integer array `invocations`, where <code>invocations[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that method <code>a<sub>i</sub></code> invokes method <code>b<sub>i</sub></code>. |
| 11 | + |
| 12 | +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. |
| 13 | + |
| 14 | +A group of methods can only be removed if no method **outside** the group invokes any methods **within** it. |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 18 | +**Example 1:** |
| 19 | + |
| 20 | +**Input:** n = 4, k = 1, invocations = \[\[1,2],[0,1],[3,2]] |
| 21 | + |
| 22 | +**Output:** [0,1,2,3] |
| 23 | + |
| 24 | +**Explanation:** |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +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. |
| 29 | + |
| 30 | +**Example 2:** |
| 31 | + |
| 32 | +**Input:** n = 5, k = 0, invocations = \[\[1,2],[0,2],[0,1],[3,4]] |
| 33 | + |
| 34 | +**Output:** [3,4] |
| 35 | + |
| 36 | +**Explanation:** |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +Methods 0, 1, and 2 are suspicious and they are not directly invoked by any other method. We can remove them. |
| 41 | + |
| 42 | +**Example 3:** |
| 43 | + |
| 44 | +**Input:** n = 3, k = 2, invocations = \[\[1,2],[0,1],[2,0]] |
| 45 | + |
| 46 | +**Output:** [] |
| 47 | + |
| 48 | +**Explanation:** |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +All methods are suspicious. We can remove them. |
| 53 | + |
| 54 | +**Constraints:** |
| 55 | + |
| 56 | +* <code>1 <= n <= 10<sup>5</sup></code> |
| 57 | +* `0 <= k <= n - 1` |
| 58 | +* <code>0 <= invocations.length <= 2 * 10<sup>5</sup></code> |
| 59 | +* <code>invocations[i] == [a<sub>i</sub>, b<sub>i</sub>]</code> |
| 60 | +* <code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code> |
| 61 | +* <code>a<sub>i</sub> != b<sub>i</sub></code> |
| 62 | +* `invocations[i] != invocations[j]` |
| 63 | + |
| 64 | +## Solution |
| 65 | + |
| 66 | +```kotlin |
| 67 | +class Solution { |
| 68 | + private lateinit var graph: Array<IntArray?> |
| 69 | + private lateinit var suspicious: BooleanArray |
| 70 | + private lateinit var visited: BooleanArray |
| 71 | + |
| 72 | + fun remainingMethods(n: Int, k: Int, invocations: Array<IntArray>): List<Int> { |
| 73 | + pack(invocations, n) |
| 74 | + suspicious = BooleanArray(n) |
| 75 | + visited = BooleanArray(n) |
| 76 | + dfs(k, true) |
| 77 | + visited.fill(false) |
| 78 | + for (i in 0 until n) { |
| 79 | + if (!suspicious[i] && dfs2(i)) { |
| 80 | + visited.fill(false) |
| 81 | + dfs(k, false) |
| 82 | + break |
| 83 | + } |
| 84 | + } |
| 85 | + val rst = ArrayList<Int>() |
| 86 | + for (i in 0 until n) { |
| 87 | + if (!suspicious[i]) { |
| 88 | + rst.add(i) |
| 89 | + } |
| 90 | + } |
| 91 | + return rst |
| 92 | + } |
| 93 | + |
| 94 | + fun dfs(u: Int, sus: Boolean) { |
| 95 | + if (visited[u]) { |
| 96 | + return |
| 97 | + } |
| 98 | + visited[u] = true |
| 99 | + suspicious[u] = sus |
| 100 | + for (v in graph[u]!!) { |
| 101 | + dfs(v, sus) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + fun dfs2(u: Int): Boolean { |
| 106 | + if (suspicious[u]) { |
| 107 | + return true |
| 108 | + } |
| 109 | + if (visited[u]) { |
| 110 | + return false |
| 111 | + } |
| 112 | + visited[u] = true |
| 113 | + for (v in graph[u]!!) { |
| 114 | + if (dfs2(v)) { |
| 115 | + return true |
| 116 | + } |
| 117 | + } |
| 118 | + return false |
| 119 | + } |
| 120 | + |
| 121 | + private fun pack(edges: Array<IntArray>, n: Int) { |
| 122 | + val adj = IntArray(n) |
| 123 | + for (edge in edges) { |
| 124 | + adj[edge[0]]++ |
| 125 | + } |
| 126 | + graph = arrayOfNulls<IntArray>(n) |
| 127 | + for (i in 0 until n) { |
| 128 | + graph[i] = IntArray(adj[i]) |
| 129 | + } |
| 130 | + for (edge in edges) { |
| 131 | + graph[edge[0]]!![--adj[edge[0]]] = edge[1] |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
0 commit comments