-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove-methods-from-project.py
36 lines (29 loc) · 1.69 KB
/
remove-methods-from-project.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from collections import defaultdict
from typing import List
class Solution:
def remainingMethods(self, n: int, k: int, invocations: List[List[int]]) -> List[int]:
# Step 1: Build the invocation graph and reverse graph (invoker -> invoked)
invocation_graph = defaultdict(list) # Regular graph: a -> b (a invokes b)
reverse_invocation_graph = defaultdict(list) # Reverse graph: b -> a (b invoked by a)
for invoker, invoked in invocations:
invocation_graph[invoker].append(invoked)
reverse_invocation_graph[invoked].append(invoker)
# Step 2: Use DFS to find all suspicious methods starting from method k
suspicious_methods = set() # Set to store all suspicious methods
stack = [k]
while stack:
current_method = stack.pop()
if current_method not in suspicious_methods:
suspicious_methods.add(current_method)
for neighbor in invocation_graph[current_method]:
if neighbor not in suspicious_methods:
stack.append(neighbor)
# Step 3: Check for external invocations (non-suspicious methods invoking suspicious ones)
for method in suspicious_methods:
for invoker in reverse_invocation_graph[method]:
if invoker not in suspicious_methods:
# If a non-suspicious method invokes a suspicious method, return all methods
return list(range(n))
# Step 4: Return all non-suspicious methods
remaining_methods = [i for i in range(n) if i not in suspicious_methods]
return remaining_methods