forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-methods-from-project.py
More file actions
32 lines (30 loc) · 921 Bytes
/
remove-methods-from-project.py
File metadata and controls
32 lines (30 loc) · 921 Bytes
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
# Time: O(n + e)
# Space: O(n + e)
# bfs
class Solution(object):
def remainingMethods(self, n, k, invocations):
"""
:type n: int
:type k: int
:type invocations: List[List[int]]
:rtype: List[int]
"""
def bfs():
lookup = [False]*n
lookup[k] = True
q = [k]
while q:
new_q = []
for u in q:
for v in adj[u]:
if lookup[v]:
continue
lookup[v] = True
new_q.append(v)
q = new_q
return lookup
adj = [[] for _ in xrange(n)]
for u, v in invocations:
adj[u].append(v)
lookup = bfs()
return [u for u in xrange(n) if not lookup[u]] if all(lookup[u] == lookup[v] for u, v in invocations) else range(n)