-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0802.py
More file actions
20 lines (19 loc) · 716 Bytes
/
0802.py
File metadata and controls
20 lines (19 loc) · 716 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
def eventualSafeNodes(self, graph: List[List[int]]) -> List[int]:
table = collections.defaultdict(list)
indegree = [0]*len(graph)
for i, nodes in enumerate(graph):
indegree[i] = len(nodes)
for node in nodes:
table[node].append(i)
q = collections.deque()
for i, j in enumerate(indegree):
if j == 0:
q.append(i)
while q:
node = q.popleft()
for next_node in table[node]:
indegree[next_node] -= 1
if indegree[next_node] == 0:
q.append(next_node)
return [i for i, j in enumerate(indegree) if j == 0]