Problem Description
You are given n methods (numbered from 0 to n - 1) and an integer k where method k has a known bug. Every invocation relationship is given in the 2D array invocations where an edge [a, b] means method a calls method b. Any method that is either k or invoked (directly or indirectly) by k is considered suspicious. However, a group of suspicious methods may only be completely removed if no method outside the group calls a method inside the group. Return an array of methods remaining after removing the suspicious ones if removal can be performed; otherwise, return all methods.
Key Insights
- Use graph traversal (DFS or BFS) starting from k to identify all suspicious methods.
- The directed relationships form a graph; traversing from k marks all methods it can reach.
- Check for any invocation edge from a non-suspicious method into a suspicious one. If such an edge exists, removal is not allowed.
- If removal is valid, output all methods that are not suspicious; if not, return the entire list of methods.
Space and Time Complexity
Time Complexity: O(n + m), where n is the number of methods and m is the number of invocations.
Space Complexity: O(n + m) to store the graph and additional data structures used for traversal.
Solution
The approach involves two main phases:
- Graph Traversal: Build an adjacency list from the invocations and perform a BFS (or DFS) beginning from node k to mark all suspicious methods.
- Validation and Removal: Iterate over each invocation. If any invocation goes from a non-suspicious method to a suspicious method, removal isn’t allowed. Otherwise, filter out the suspicious methods and return the remaining methods.
Key data structures include:
- A graph (adjacency list) to represent the method call relationships.
- A set or boolean array to keep track of suspicious methods.
- A queue (for BFS) to traverse the graph from k.
This method ensures that you efficiently find suspicious methods and quickly determine if removal is feasible.